I am using the attached function to execute a command line and to wait until the executed program is done before proceeding forward. When I use it for MS Excel or Adobe PDF reader, it functions as expected. When I try to execute WINWORD and pass as a parameter the path to the document that is to be opened, WaitForSingleObject returns immediately, even before the WORD program shows on the screen.
Normally I delete the temporary file that is being opened by the called process once the process has completed. the fact that WINWORD is returning immediately is causing an obvious problem.
If I place a breakpoint right after WaitForSingleObject is called, Word eventually shows.
Does anybody know why Word is acting this way .... and more importantly does anybody know how I can program around this?
Public Sub ExecuteAndWait(cmdline As String, bShowMaximized As Boolean)
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim X As Long
' Set up the parameters that will be used for the subsequent calls.
If bShowMaximized = True Then
NameStart.dwFlags = STARTF_USESHOWWINDOW
NameStart.wShowWindow = SW_SHOWMAXIMIZED
NameStart.cb = Len(NameStart)
' Make the API calls.
X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
X = CloseHandle(NameOfProc.hProcess)