Call Shell wait before finish

B

Boon

Hello,

I have a command...

Call Shell("Run.bat", 1)
..
..
..



The Shell command will call another program to run. I would like the VBA to
wait until the program finish running before execute the next command. How
can I do this?

thank you!
Boon
 
D

Dorian

Here's some code I used to do it:

Public Sub ShellFTP(tf1 As Variant)
' Executes FTP upload script
On Error GoTo ER
Dim x As Boolean
x = ShellAndWait("FTP -i -s:C:\Exports\TWS\FTPload.txt", tf1)
Exit Sub
ER:
tf1.WriteLine ("Error in ShellFTP: " & Err.Number)
tf1.WriteLine (Err.Description)
End Sub

Public Function ShellAndWait(ByVal program_name As String, tf1 As Variant)
As Boolean
' Start the indicated program and wait for it to finish, hiding while we
wait.
On Error GoTo ER
Dim process_id As Long, process_handle As Long
' Start the program
process_id = Shell(program_name, 0)
On Error GoTo 0
DoEvents
' Wait for the program to finish.
' Get the process handle.
process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
If process_handle <> 0 Then
WaitForSingleObject process_handle, INFINITE
CloseHandle process_handle
End If
ShellAndWait = True
Exit Function
ER:
tf1.WriteLine ("Error in ShellAndWait: " & Err.Number)
tf1.WriteLine (Err.Description)
ShellAndWait = False
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top