Shelling Out to a batch file

Mechanic

Board Regular
Joined
Mar 26, 2002
Messages
60
What are the Excel Visual Basic command(s)I should use to Shell out to a DOS batch program? "Shell" doesn't seem to work.

Mechanic
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
try
Sub RunPcDownloadprodreview() '
'
'
Dim TaskID As Long
Dim hProc As Long
Dim lExitCode As Long
Dim access_Type As Variant
Dim STILL_ACTIVE As Variant
Dim Program As Variant

access_Type = &H400
STILL_ACTIVE = &H103
ChDir (wkbname)
Program = wkbname & "filename.bat"
TaskID = Shell(Program, 1)
hProc = OpenProcess(access_Type, False, TaskID)
Do
GetExitCodeProcess hProc, lExitCode
DoEvents
Loop While lExitCode = STILL_ACTIVE

End Sub

this will hold the window open till the bat file finishes.
 
Upvote 0
I tried the code you gave me and receive an error on OpenProcess command. It tells me that it doesn't recognize the function or Sub program.

Mechanic
 
Upvote 0
sorry mate add this to your code
at the top in the declarations
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
 
Upvote 0
How about something like this which uses the Shell function:

'<pre>

Dim sbatfilepath As String
Dim sBatFile As String


sBatFile = "test.bat"
sbatfilepath = "C:Temp"

ChDir sbatfilepath

'You might want to put a test in to make sure that the .bat file exists
If Environ("OS") <> "" Then
' It's NT
Shell "cmd /C " & sBatFile, 1
Else
' It's 95 or 98
Shell "COMMAND.COM /C " & sBatFile, 1
End If

End Sub '</pre>

HTH
 
Upvote 0
By the way,
What does the "COMMAND" part of the Shell statement look like for Windows 2000?

Mechanic
 
Upvote 0
It's "cmd" as well. You can test this out by going to Start|Run (or WINDOWS KEY + R, if you want to be flash) and type in "cmd" into the run dialogue box.
 
Upvote 0
Windows 2000 is also known as NT5, so Environ("OS") returns:<pre>
Windows_NT</pre>

MS are good at hitting their own deadlines and keeping naming conventions going, hence the two different names for the OS. :)

I'll need to test ME when I go home, but I'm pretty sure everything in the Win9x family, which ME is does not have Environment parameters. That's why the test is just Environ("OS")<>"".
_________________<font color = green> Mark O'Brien
This message was edited by Mark O'Brien on 2002-04-16 11:42
This message was edited by Mark O'Brien on 2002-04-16 11:44
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,938
Members
448,534
Latest member
benefuexx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top