Running a batch file from VB

  • Thread starter Thread starter Graham Carter
  • Start date Start date
G

Graham Carter

I am trying to execute a batch file from visual basic.
While the VB help suggests ways to execute DLL or enable
OLE objects, these are well beyond my skill area. I can't
find anything specific in help to allow me to run a batch
file automatically.

When running the program manually, I open a command prompt
window and run the batch file from there. I would like to
automate this from Excel.

Thx in advance, GC
p.s. How do you search the newsgroup aside from scrolling
through the postings?
 
Graham Carter said:
I noticed the posting from this mroning on printing PDF
files and managed to get the batch file to run using
ShellExecute. Now I just have to figure out how to change
the directory before runnign the batch file and how to
close the command prompt shell ...

Use the Shell function, the VBA help file
describes it

Shell(pathname[,windowstyle])

Pathname is the full pathname of the program

e.g.

' Specifying 1 as the second argument opens the application in
' normal size and gives it the focus.
Dim RetVal
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1) ' Run Calculator.
Keith
 
I've used the Shell() command in the past, and it is OK ... However, I
have found that using the 'Windows Script Host' is better, as you can
wait until the actual batch file has completed, before continuing.

You first need to add the 'Windows Script Host Object Model' to the
list of references, and then you need the following code:

Dim WSHShell As New IWshShell_Class

'ARA run the calculator, wait for it to be closed before
continuing.
Call WSHShell.Run("calc.exe", , True)


Have Fun,


Alex.
 
Hi

Use something like this to select the drive and dir you want.

ChDrive "D" ' be sure you are logged on to the drive you want, in this
case drive "D"
ChDir "\my music" ' change to the dir you want to be in, starting first at
the root dir (the \)


MsgBox CurDir ' show me where I am


To close the dos window after the batch file ends, I assume you are using
win 98 since win xp seems to do this as the default.

After running the batch file, with the dos window open, right click on the
dos window title bar. Select properties. On one of the tabs(I forget which
one and am not on a win 98 system) you will see a checkbox with the caption
" Close on Exit". Check the checkbox. Click apply. Click OK.

Now it should close automatically after it finishes running.


HTH

Ken M
 
Back
Top