Microsoft Office Tutorials and References
In Depth Information
Inside Out
Simulate a Pause in VBA code
There might be times when you are required to launch an application; however, you’ll want
to pause your VBA code until the application is closed. For example, the application
launched is creating a file that is required later in your procedure. Because you’re unable to
pause your code, you can work around this situation by programming a loop that monitors
the applications status. The following procedure was designed to display a message box
when the application launched by the Shell function is no longer active:
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
Sub RunCharMap()
Dim TaskID As Long
Dim hProc As Long
Dim lExitCode As Long
Program = "Charmap.exe"
On Error Resume Next
TaskID = Shell(Program, vbNormalFocus)
hProc = OpenProcess(ACCESS_TYPE, False, TaskID)
If Err <> 0 Then
MsgBox "Unable to start " & Program, vbCritical, "Error"
Exit Sub
End If
GetExitCodeProcess hProc, lExitCode
Loop While lExitCode = STILL_ACTIVE
MsgBox Program & " is no longer the active application"
End Sub
While the launched program is active, the procedure continues to loop until the lExitCode
returns a different value. When the loop ends, the VBA code will resume.
Search JabSto ::

Custom Search