Go Premium for a chance to win a PS4. Enter to Win


WaitForSingleObject not waiting when executing WinWord.

Posted on 2008-06-10
Medium Priority
Last Modified: 2008-06-12
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 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
    End If
    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)
End Sub

Open in new window

Question by:dzumwalt
  • 5
  • 5
  • 2
LVL 26

Expert Comment

ID: 21758965
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run """C:\Program Files\Microsoft Office\Office12\winword.exe""", , True
Debug.Print Now
Set wshShell = Nothing
LVL 29

Expert Comment

ID: 21758988
Could you post the command line you are trying to pass to ExecuteAndWait.
I notice you are using the ANSI version of CreateProcess however your passing a long value for lpApplicationName when it should be a VbNullString or ("winword.exe") so could you post the API declerations for CreateProcess as well and I could help
LVL 29

Expert Comment

ID: 21760675
For the code you are using the command line should look like this

ExecuteAndWait "C:\Program Files\Microsoft Office\Office\winword.exe ""C:\Documents and Settings\Administrator\test doc.doc""", True

You need to put your command line into double quotes like displayed above. Also there is a much better way to execute files and documents using ShellExecute API WaitForSingleObject won't allow windows messages to process to your application if you display any type of user interface it won't allow any type of input or repaint and could be a very nasty white mess on the screen until the application your pending has been closed.This is when you should use MsgWaitForMultipleObjects because it will allow you to process window messages to your user interface.

I have written a simple example that can take a file document or execuatable and open it so that your application stays responsive during the process.

Option Explicit
Private Const SEE_MASK_NOCLOSEPROCESS   As Long = &H40
Private Const WAIT_OBJECT_0             As Long = 0
Private Const INFINITE                  As Long = &HFFFF
Private Const QS_ALLINPUT& = (&H40 _
    Or &H20 _
    Or &H10 _
    Or &H8 _
    Or &H4 _
    Or &H2 _
    Or &H80 _
    Or &H1)
        cbSize          As Long
        fMask           As Long
        hwnd            As Long
        lpVerb          As String
        lpFile          As String
        lpParameters    As String
        lpDirectory     As String
        nShow           As Long
        hInstApp        As Long
        lpIDList        As Long
        lpClass         As String
        hkeyClass       As Long
        dwHotKey        As Long
        hIcon           As Long
        hProcess        As Long
End Type
Private Declare Sub RtlZeroMemory Lib "kernel32.dll" ( _
    Destination As Any, _
    ByVal Length As Long)
Private Declare Function ShellExecuteEx Lib "shell32.dll" ( _
    lpExecInfo As SHELLEXECUTEINFO) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
    ByVal hObject As Long) As Long
Private Declare Function MsgWaitForMultipleObjects Lib "user32" ( _
    ByVal nCount As Long, _
    pHandles As Long, _
    ByVal fWaitAll As Long, _
    ByVal dwMilliseconds As Long, _
    ByVal dwWakeMask As Long) As Long
Private Declare Function PathFileExistsW Lib "shlwapi.dll " ( _
    ByVal pszPath As Long) As Boolean
Public Sub ShellExecuteWait(ByVal ProgramPath As String)
    Dim lBusy As Long
        ' Check if the file exists
    If PathFileExistsW(StrPtr(ProgramPath)) Then
        ' Initiate struct
        RtlZeroMemory ExecuteInfo, Len(ExecuteInfo)
        ' Setup struct
        With ExecuteInfo
        .cbSize = Len(ExecuteInfo)
        .lpFile = ProgramPath
        .lpVerb = "open"
        .nShow = 1
        .lpDirectory = Left$(ProgramPath, InStrRev(ProgramPath, "\") - 1)
        End With
        ' Pass struct info
        Call ShellExecuteEx(ExecuteInfo)
        ' Pend process event signal
          lBusy = MsgWaitForMultipleObjects(1, ExecuteInfo.hProcess, False, INFINITE, QS_ALLINPUT)
        Loop Until lBusy = WAIT_OBJECT_0
        ' Close handle
        CloseHandle ExecuteInfo.hProcess
        'Display some info...
        Debug.Print ExecuteInfo.lpFile & " has been closed " & Time()
    End If
End Sub
Private Sub Command1_Click()
   ShellExecuteWait "C:\Documents and Settings\Administrator\test doc.doc"
End Sub

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 21762282
Thanks egl1044 for your input!

While it is much more convenient to have the doevents executing and thus I don't have that white screen you mentioned in my original application, the behavior when executing winword is exactly the same.

As I said before, when I am executing other programs suich as a PDF reader, or Microsoft Excel, MsgWaitForMultipleObjects will cause the process to wait until the program complete. WinWord seems to be different though. It's seems like the EXE I am calling is spawning another executable and then immediately terminating which then causes MsgWaitForMultipleObjects to indicate the process is complete  (pure conjecture on my part - I have no data to back that up).

If I place a breakpoint on the line where you are closing the handle, and if I just wait for about 10 seconds, my document appears on the screen in Microsoft Word!

Here is the actual path to the executable I am using:

C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE

... and here is the parameter string that I am placing in ExecuteInfo.lpParameters:


Is my assumption above correct? If so, how can handle that?

Author Comment

ID: 21762290
Increasing point value of this to 400.
LVL 29

Accepted Solution

nffvrxqgrcfqvvc earned 2000 total points
ID: 21762636
Without adding anything else to the ExecuteInfo and leaving the code as it was posted and passing directly to the file:

ShellExecuteWait "C:\DOCUME~1\Dan\LOCALS~1\Temp\TEMP_886577\327_brillvia1-28final.RTF"

This works on my system by launching the RTF document with word. You can just pass the filename to the routine and it will launch it using the defualt applicaton associated with the document. I can confirm on my system that this works without any problems. Have you tried the routine like this?
LVL 26

Expert Comment

ID: 21762664
do you have to use waitforsingleobject?

Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run """C:\Program Files\Microsoft Office\Office12\winword.exe "" ""C:\Documents and Settings\Administrator\test doc.doc""", , True
Debug.Print Now
Set wshShell = Nothing
LVL 29

Expert Comment

ID: 21762861
I think I know what is happening.

You already have an instance of WINWORD.EXE open and when you make the call hProcess = 0 so WaitforSingleObject and MsgWaitforMultipleObjects are not waiting on any type of event to occur because the process failed. However if there is no instances open of WINWORD.EXE it works.

To me it seems as if you open word externally from another application or using your mouse and since you have the instance open already your calls fail to obtain the process handle making any type of waiting function fail

A handle to the newly started application. This member is set on return and is always NULL unless fMask is set to SEE_MASK_NOCLOSEPROCESS. Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.


Author Comment

ID: 21762936
Interesting. So under this scenario I don't have to first determine the program that is associated with the file extension and then call this process. I just pass in the path to the file and leave it to ShellExecuteEx to figure out the proper program to run.

That worked. Good job! Thanks egl1044!

Author Comment

ID: 21763015
Pursuant to your last comment egl1004 I did another test.

I started word with a blank document. I minimized it.

I went to my application and "opened" the rtf.

This caused a second instance of Word to appear in my taskbar. MsgWaitForMultipleObjects continued to report BUSY

I exited the RTF document. MsgWaitForMultipleObjects still reported BUSY. The original instance of Word was still running.

I exited the original instance of Word. MsgWaitForMultipleObjects never released.

Comments? (boosting to 500 points now)

Author Comment

ID: 21772414
Well .... no reply to the last post by me. Still ... egl1044 helped out a lot and so I will award points to him/her.
LVL 29

Expert Comment

ID: 21773517
Thank you dzumwalt.

Replying to your last comment, This is how the the API  is setup to perform and you would have to add additional checksums for sitiuations like these. The problem is that if the application is already running it will not return a process handle so the function fails. If the application is not running it will get the handle and work as intended.

Possible Methods:

1) Enumerate the windows on the system and match a ClassName and alert the user to please close all running instances of said application before running the procedure.( see EnumWindows,GetWindowClassName)

2) Another option would be to enumerate the process currently running and match the EXE name WINWORD.EXE and then setup your own events by obtaining the real process handle of the already active handle and wait on that(CreateEvent,OpenProcess)  this would take alot of extra work.

3) Simply only using the funtion to execute a process you know the user wouldn't use on a daily basis, adding a reference to a word component so you can automate from your vb application

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question