|
Answer» Hi there,
I am trying to run a very simple batch file from VBA. Here is the cotents of the batch file:
dir c:\ > try.txt
When I run the batch file by itself, the text file is created.
Then I want to launch it from VBA using the following command:
Shell("C:\Documents and Settings\Administrateur\try.bat", vbNormalFocus)
A MS-DOS window opens but the text file is not created.
Could you please help me with this simple task?
Thanks in advance,
Benny. How about this?
(Also - are you looking in the right folder for the text file?)
Code: [Select] Dim ret As Integer ret = Shell "C:\Documents and Settings\Administrateur\try.bat"
If you want to wait for your batch to complete before continuing, make sure it includes and END or EXIT and use this:
Code: [Select] Dim ret As Integer ret = CALL Shell "C:\Documents and Settings\Administrateur\try.bat"
Hi,
Thx for your prompt reply, unfortunatly, it's STILL not working. when I run the batch file alone, the text file is created in C:\Documents and Settings\Administrateur folder, I expect to get it in the same folder when I run from vba. The command "Call" is not recognized by my VBA (running with Excel).
Any suggestions? Thanks a LOT for your help. This seems to be a way of running a batch on workbook open. MAYBE you could adapt it...
Implement this code in Workbook_Open. The /c closes the DOS prompt when finished.
Code: [Select] Private Sub Workbook_Open()
Call Shell(Environ$("COMSPEC") & " /c C:\Path.bat", vbNormalFocus)
End Sub
or this...
Code: [Select]Put THIS code in the ThisWorkBook:
Sub TEST()
Shell32Bit "C:\test.bat", "WAIT"
End Sub
see here
http://www.google.co.uk/search?source=ig&hl=en&q=run+.bat+from+vba+excel&meta= Oki the first method is working! Thanks lot for your help!
|