Logging Error VBA excel Batch command - excel

I have a batch file contains
echo test string >>Log.txt
when i click on it it will make a Log.txt file and the "test string" will be in it. But when i run the batch file from VBA excel using WScript.Shell it is not creating the Log.txt file. But the batch code is running. How can I make it working? I am using windows7 and excel 2007

I suggest specifying an absolute path to the file.
echo test string >>"%USERPROFILE%\Document\Log.txt"
The most common frustration I experience when using relative paths is that it's relative to the "working directory" windows uses for the calling process, and if it's launched from Explorer, that can easily be %WINDIR% or some place where you don't want stuff saved.

Related

Batch file creation: Convert xls to csv using only batch script

I have done quite a bit of searching before posting this question so let me outline what I am trying to do.
1.) I do not want to use applications I have to download from a website or created custom commands (please no start Xls2Csv.exe here's a link to a website where you can download the program) I do not want to download a program to do this.
2.) I want to keep it in the batch file if possible - I have tried the vbc/vbs/vb files that is not what I am looking for.
3.) I found this an this is close to what I need but if I can stay within a batch file that would be best: Can a Batch File Tell a program to save a file as? (If so how)
Background
I have a bunch of test records stored in excel sheets within folders. Each test record has autoformatted name so the only real difference between any of the filenames is a serial number, otherwise each file name is formatted the exact same way.
I have written a batch file to search and find the files I need but I am stuck on obtaining a tiny bit of information in a .xls file.
What I am trying to do - I have excel files (.xls) and there is a word in a cell on one of many sheets that I would like to copy into a textfile. However I am unable to use findstr for an excel find because the command searches the file as if you opened it in notepad and the data I need is not present.
I am not concerned of data loss as long as I can get this tiny bit of information to a text file.
Otherwise what I have found to be the best solution is to convert an XLS to a CSV. I have manually done it by opening the file and saving as type .csv that worked.
What hasn't worked is:
example1.xls >> example2.csv
ren example1.xls example3.csv - this will save it as a csv file but still opens with the same formating of the xls file in both excel and notepad.
I was hoping that the was a command to recreate the manual process of opening the file and saving as csv.
If there are any other suggested solutions - maybe a command where I can search for a string within an excel file? That would be the simplest option.

Batch file command to open excel\csv as notepad

I can open an excel file from .bat by simply using the path of the file.
Is there a parameter to open the csv in edit mode (as notepad) ?
As Squashman mentioned in the comments, if you are just in the command prompt and need to edit any file, you can simply run notepad <filename>. However, if you need this to be part of a larger batch file or want it to be in batch form for another reason, you can use the below example which does a couple notable things:
Accepts the filename as a parameter.
Uses START so that the batch file will exit/resume immediately. If you leave this out, the batch file will wait for you to close notepad.
editfile.bat
#echo off
set "f=%1"
echo Opening %f% in notepad.exe
START notepad %f%
Usage: editfile filename.csv
Obviously, it doesn't have to be a CSV file. It can be any file.

How to create silent install using Winrar for Setup program 'Run after installation'

I can create normal silent executable by selecting 'Hide all' option in Silent mode through SFX options.
But when I add file in SFX options -> Setup program ->'Run after extraction' e.g. devnode.exe
devnode.exe does not run silently.
Is there way to run setup file silently?
Windows opens automatically a console window if devnode.exe is a console application and not a Windows (GUI) application. This can't be avoided. Only Windows application can be executed without showing a window if the application is coded not opening a window on execution.
However, usually it is good practice to show a user executing a self-extracting archive what is going on. The requirement of a completely hidden installation using a self-extracting archive is something mainly bad guys need.
A good installation of an application which need to run a console application is done best with running a batch file after extracting the files showing with 1 or more echo messages what happens now before running the console application from within the batch file and delete with last line in batch file the batch file itself.
Example of such a setup/install batch file:
#echo off
rem Set title for the console window.
title Installation of XXX
rem Output information for the user.
echo Installing XXX, please wait ...
rem Call the console application which completes the installation.
devnode.exe
rem Delete this batch file as not needed anymore.
del %0
Note: The batch file is executed always with surrounding double quotes and therefore %0 is a string containing already double quotes at beginning and end. So it would be wrong to use double quotes around %0 in the last line.

How can I pass a file to an executable, the way dragging-n-dropping allows?

Consider I have the following :
An Excel workbook, which generates a .txt file (through a macro).
An executable file (.exe) which can parse this .txt file.
My objective here is to program the following algorithm :
Get the user to enter his data in the worksheets.
Prepare the .txt file according to what the user entered.
Pass the newly created .txt file to the external program (.exe).
Now, the first two steps work perfectly, I can generate the proper .txt file. The problem comes when I need to pass it to the external executable. Here's what's tried and observed :
Calling Shell() with : "C:\path\to\program.exe" "C:\path\to\file.txt" (EDIT : as a single string) ; simple enough, yet this does not work : the external .exe doesn't like being given a path. I have also tried entering the command directly into Windows' Execute utility : same. It seems like giving the program a path to the .txt isn't enough.
Dragging the .txt file onto the program's .exe works! The external executable does its work correctly. Here, it is as if I had passed the "file" to the program, not just its path. As a UNIX user, this sound like quite a non-sense to me (without a notion of stream or pipe), so maybe I'm missing something here...
Now, here's my question. When dragging-n-dropping a file onto an executable, how does Windows "translate" it ? Obviously, it does not translate it to "pass the file's path as the program's first parameter". How can I pass the .txt file to the executable program, without just passing its path like I did with my first Shell() call ?
A few other notes :
I am using Microsoft Office Excel 2010 on Windows 7. I'm already dying from it, thanks for your concern.
This external .exe I'm talking about cannot be modified and recompiled (which would be awesome), that's the tricky thing. I have to use it as it is, whatever happens.
Using Shell() is not a problem, if you give me the proper command/fix. I have also tried calling Run on a VBA.CreateObject("WScript.Shell"), same result.
Now, here's my question. When dragging-n-dropping a file onto an
executable, how does Windows "translate" it ? Obviously, it does not
translate it to "pass the file's path as the program's first
parameter".
It does exactly that. Create a batch file:
#echo off
echo You passed parameter: %1
pause
Save it as test.bat
Browse to it in file explorer, and drag/drop something onto it:
Previous comments:
Looks like you have to send the path and filename as one parameter, e.g.:
Shell("C:\path\to\program.exe C:\path\to\file.txt")
or if the paths have spaces in them, maybe:
Shell("""C:\path\to\program.exe"" ""C:\path\to\file.txt""")
http://social.msdn.microsoft.com/Forums/en-US/a3a3d1a7-5c9a-4c91-ab96-41d367eac2fb/using-shell-function-in-vba-how-do-i-pass-a-parameter?forum=accessdev

Opening Excel and PDf files with Tcl Tk

I am having problems opening an existing Excel file with Tcl Tk. I am able to open an existing MS Word file with no problems. The code that I am using is as follows, also my test application has "package require tcom" included:
proc OpenFile {} {
#Path to file
set app [::tcom::ref getobject "C:\\Users\\Me\\Desktop\\Test.doc"]
#Change path to application
set this [$app Application]
#Open application
$this Visible 1
}
This code is executed by a button. Basically, Test.doc gets opened after the button is pressed.
I tried changing the file to an existing Excel file, and when I press the button the file opens for a split second, and then closes. This also happens with MS Access files, as well.
Does anyone know how to open an existing Excel file with Tcl Tk, and make it stay open? Additionally, for PDFs and text files, I understand that I cannot use Tcom to open these files. Does anyone know how to open PDFs, text, and other non-MS files with Tcl Tk?
I really appreciate your help!
Thank you,
DFM
Assuming you're on Windows and you just want to open a file (.xls, .pdf, ...) with its usual application (ie. not modifying the file from your script) you can just use "start" like this:
set TestDoc "My Test.xls"
eval exec [auto_execok start \"\" [list $TestDoc]

Resources