none
Word document close event for the very first new document RRS feed

  • Question

  • Hi,

    when Word starts it creates a new document, named Document1. Is there a way to get notified that Document1 is closed? The following event fires not for the first new document:

    ((Word.DocumentEvents2_Event)this.document).Close -=

    new Word.DocumentEvents2_CloseEventHandler(this.Document_CloseEvent);


    That event is fired for all the other documents. I really need to get notified by this event for Document1. How?

    Yavuz

    Thursday, April 24, 2014 11:20 AM

All replies

  • Hello Yavuz,

    Where do you run the code for subscribing to the event? Could you please show your sample code completely?

     

    Thursday, April 24, 2014 12:47 PM
  • Hello Eugene,

    somehow the problem is gone. I could swear that it happened like I wrote before. It is odd because I did not changed any code.

    To your question: the events are subscribed (indirectly) in the ThisAddIn_Startup event handler.

    This is a more complete example:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
      this.wrappedDocuments = new List<DocumentWrapper>();
      foreach (Word.Document document in Globals.ThisAddIn.Application.Documents)
      {
        this.wrappedDocuments.Add(this.WrapDocument(document));
      }
     
      Globals.ThisAddIn.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(this.Application_DocumentOpen);
      ((Word.ApplicationEvents4_Event)Globals.ThisAddIn.Application).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(this.Application_NewDocument);
    }
    
    private void Application_NewDocument(Word.Document document)
    {
      this.wrappedDocuments.Add(this.WrapDocument(document));
    }
     
    private void Application_DocumentOpen(Word.Document document)
    {
      this.wrappedDocuments.Add(this.WrapDocument(document));
    }
    
    private DocumentWrapper WrapDocument(Word.Document document)
    {
      var wrappedDocument = new DocumentWrapper(document);
      wrappedDocument.Closed += new DocumentWrapperClosedEventHandler(this.WrappedDocument_Closed);
      return wrappedDocument;
    }
    

    The Close event of the Document interface is subscribed in the constructor of the DocumentWrapper class.

    public DocumentWrapper(Word.Document document)
    {
      this.document = document;
      ((Word.DocumentEvents2_Event)this.document).Close += new Word.DocumentEvents2_CloseEventHandler(this.Document_CloseEvent);
     
      this.panes = new Dictionary<string, Microsoft.Office.Tools.CustomTaskPane>();
    }
    

    The method DocumentWrapper.Document_CloseEvent() was never reached for the very first new document.

    EDIT:

    This problem poped up recently as a bug. I have simplified the above code to this version:

    public partial class ThisAddIn
    {
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
        foreach (Word.Document document in Globals.ThisAddIn.Application.Documents)
        {
          ((Word.DocumentEvents2_Event)document).Close += ThisAddIn_DocumentClose;
        }
    
        Globals.ThisAddIn.Application.DocumentOpen += Application_DocumentOpen;
        ((Word.ApplicationEvents4_Event)Globals.ThisAddIn.Application).NewDocument += ThisAddIn_NewDocument;
      }
    
      private void ThisAddIn_NewDocument(Word.Document document)
      {
        ((Word.DocumentEvents2_Event)document).Close += ThisAddIn_DocumentClose;
      }
    
      private void Application_DocumentOpen(Word.Document document)
      {
        ((Word.DocumentEvents2_Event)document).Close += ThisAddIn_DocumentClose;
      }
    
      private void ThisAddIn_DocumentClose()
      {
      }
    
      private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
      {
      }
    
      #region VSTO generated code
    
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InternalStartup()
      {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
      }
            
      #endregion
    }

    When Word starts there exists Document1. If Menu File -> Close is clicked the window of Document1 is closed and the close event is fired. This is expected.

    When Word starts there exists Document1. User does not change Document1. User opens an existing document. The window of Document1 is closed and a new window for the opening document is opened. The close event is not fired. This is not expected and leads to an exception in my application.


    • Marked as answer by Marvin_Guo Tuesday, May 6, 2014 2:02 AM
    • Unmarked as answer by Y.avuz Tuesday, October 21, 2014 11:36 AM
    • Edited by Y.avuz Tuesday, October 21, 2014 12:27 PM
    Friday, April 25, 2014 7:10 AM