ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Running a batch file from VB (https://www.excelbanter.com/excel-programming/275599-running-batch-file-vbulletin.html)

Graham Carter

Running a batch file from VB
 
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?

Keith Willshaw

Running a batch file from VB
 

"Graham Carter" wrote in message
...
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



Alex[_8_]

Running a batch file from VB
 
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.

Ken Macksey

Running a batch file from VB
 
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



"Graham Carter" wrote in message
...
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?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 19/08/2003




All times are GMT +1. The time now is 04:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com