|
|
Title | Start another program using Shell and wait until it finishes |
Description | |
Keywords | Shell, start, execute, wait, finish |
Categories | Windows |
|
|
The ShellAndWait subroutine uses the Shell function to start the other program. It calls the OpenProcess API function to connect to the new process and then uses WaitForSingleObject to wait until the other process terminates. Note that neither the program nor the development environment can take action during this wait.
After WaitForSingleObject returns, the ShellAndWait subroutine calls CloseHandle to close the process handle opened by OpenProcess and then exits at which point the program resumes normal execution.
|
|
' Start the indicated program and wait for it
' to finish, hiding while we wait.
Private Sub ShellAndWait(ByVal program_name As String, _
ByVal window_style As VbAppWinStyle)
Dim process_id As Long
Dim process_handle As Long
' Start the program.
On Error GoTo ShellError
process_id = Shell(program_name, window_style)
On Error GoTo 0
' Hide.
Me.Visible = False
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
' Reappear.
Me.Visible = True
Exit Sub
ShellError:
MsgBox "Error starting task " & _
txtProgram.Text & vbCrLf & _
Err.Description, vbOKOnly Or vbExclamation, _
"Error"
End Sub
|
|
Some people have reported trouble using Shell() in VBA with Access 2002. In some cases, it may return True or False rather than a task handle. Some people report this working just fine, however, so who knows what the issue is.
See also:
|
|
|
|
|
|