2
votes

My application uses Word interop for "active reporting" functionality. When the Word document is launched from the application, we set a EventWaitHandle to pause the application (creating a 'modal' effect) until the document is closed:

 wh.WaitOne();

We have set an event on the word Application Quit event, where we then set the wait EventWaitHandle for the application to continue

wordGenerator.WordApplication.ApplicationEvents2_Event_Quit += WordApplication_ApplQuit;

private void WordApplication_ApplQuit()
    {
        wh.Set(); // signal that word has closed

        wordGenerator.Dispose();
        wordGenerator = null;
    }

After this is called, the application then reads the document from the location it was stored and saves it into our database. All works great. UNLESS... the user makes changes in the document and doesn't CTRL+S but rather clicks close and gets prompted with the "would you like to save changes" prompt.

What happens in this instance that the quit event is fired as soon as you click close in Word, but Word is still open whilst the dialog to save changes is there. The application then continues to run and gets IO exceptions "Document is in use by another process" when trying to read the document to save to the database. Even waiting and retrying doesn't work as it seems that Word and the Application are waiting on eachother.

Is there another event I can use? I can't bypass the alert and automatically save as perhaps the user doesn't want to save.

1
Can you get the Process object for Word?It'sNotALie.

1 Answers

1
votes

Problem solved... easy one this time. Moved the dispose code above the .Set().

private void WordApplication_ApplQuit()
{

    wordGenerator.Dispose();
    wordGenerator = null;

    wh.Set(); // signal that word has closed

}