I'd be interested in what everyone thinks about this solution. It's similar to the one that nyx uses, but avoids having to associate a Process ID with a Process Handle. Basically, we keep activating the new process until we can't activate it any more. This returns an error which tells allows the ShellAndWait procedure to continue.
Also, Microsoft mentions a possible solution at:
This makes use of the Tasks collection, but I was unable to find a reference for this collection. Can anyone shed some light on this?
The procedures:
Public Function ShellAndWait(PathName As String)
Dim TaskID As Double
'Run application and return Process ID (TaskID)
TaskID = Shell(PathName, vbNormalFocus)
While TaskExists(TaskID)
'Wait for TaskExists False
Wend
End Sub
Public Function TaskExists(TaskID As Double) As Boolean
'Raise Error if TaskID does not exist
On Error GoTo ErrorHandler
'Attempt to activate TaskID
AppActivate (TaskID)
'If AppActivate returns an error, we know that the
'process is finished. We handle the error and return
'the function as false--this allows the calling
'procedure to exit the While loop.
ErrorHandler: If Err.Number = 5 Then
TaskExists = False
Exit Function
Else
TaskExists = True
End If
End Function