title: Word Interop: Event to catch close of Word
body: <p>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:</p>
  
  <pre><code> wh.WaitOne();
  </code></pre>
  
  <p>We have set an event on the word Application Quit event, where we then set the wait EventWaitHandle for the application to continue</p>
  
  <pre><code>wordGenerator.WordApplication.ApplicationEvents2_Event_Quit += WordApplication_ApplQuit;
  
  private void WordApplication_ApplQuit()
      {
          wh.Set(); // signal that word has closed
  
          wordGenerator.Dispose();
          wordGenerator = null;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>