If you want to execute an application and waiting for its termination, you can follow two approaches. The “classic” approach is based on API functions such as OpenProcess, WaitForSingleObject and CloseHandle. However, there is a simpler technique based on the Windows Scripting Host object model.
A method exposed by this library lets you to run a file, and specify whether you want to wait for its termination. To use the following code snippet you must add a reference to Windows Scripting Host Object model in the “References ” dialog window. The method you’re looking for is the Run method of the IWshShell_Class object. Its first argument is the file to run, the second specifies the styles of the application’s window and the last is a Boolean value that tells whether you want to wait for the program termination. Here’s a function that contains all the necessary code:
Sub RunAndWait(ByVal sFile As String) Dim WSHShell As New IWshShell_Class WSHShell.Run sFile, , TrueEnd Sub
To test this function, write the following code in your form (it assumes that Notepad is on your system path):
Private Sub Form_Click() RunAndWait "notepad.exe" MsgBox "Back to your application!"End Sub
You’ll see that the message box will appear only when you close Notepad.