VBA TIPS
Wait for Shell Call to Complete

You can use the following API functions when you want a user to wait for the shelled application to finish.

The OpenProcess function opens an existing process object.
The WaitForSingleObject function returns when the specified object is in the signaled state or the time-out interval elapses.
The CloseHandle function closes an open object handle.

The following code Open a Notepad then wait for the notepad is closed.

Example

Place the following in a standard module.

Option Explicit
Public Declare Function OpenProcess Lib "kernel32" ( _
                                    ByVal dwDesiredAccess As Long, _
                                    ByVal bInheritHandle As Long, _
                                    ByVal dwProcessId As Long _
                                    ) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" ( _
                                            ByVal hHandle As Long, _
                                            ByVal dwMilliseconds As Long _
                                            ) As Long
Public Declare Function CloseHandle Lib "kernel32" ( _
                                    ByVal hObject As Long _
                                    ) As Long
Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = &HFFFF

Sub WaitUntilAppClose()
    Dim lngRtn As Long
    Dim lngProID As Long
    Dim lngProHn As Long
    
    MsgBox "This program will be stopping until a NotePad is colosed."
    lngProID = Shell("NotePad.exe", 1)
    lngProHn = OpenProcess(SYNCHRONIZE, True, lngProID)
    lngRtn = WaitForSingleObject(lngProHn, INFINITE)
    lngRtn = CloseHandle(lngProHn)
    MsgBox "Note pad is closed"
End Sub

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
ABOUT
WORKS
THE CELL MASTERS
CONTACT
LINKS
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo