Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Word DocumentChange event since NewDocument event is not fired on load

Neither the NewDocument nor the DocumentOpen event is fired when Microsoft Word first loads. When an instance of Word is already open and a new or existing document is opened then these events fire fine.

The suggestion I've seen is to handle the DocumentChange event (which is always fired when Word loads) instead of the other two events.

My question is how would I go about this? The DocumentChange event does not have any parameters so how would I know when the document (new or existing) was just opened?

Additionally, I already have logic in the DocumentChange event and the processing for new and existing documents is different so I can't just throw all of my code into the event.

private void ThisAddIn_Startup(object sender, System.EventArgs a)
{
  this.Application.DocumentChange += new ApplicationEvents4_DocumentChangeEventHandler(Application_DocumentChange);
}

private void Application_DocumentChange()
{
  // How do I handle NewDocument or DocumentOpen?
}
like image 939
Joe W Avatar asked Oct 01 '12 20:10

Joe W


2 Answers

This link will help you, basically what they say is that ThisAddIn_Startup runs after the DocumentOpen event has finished running. There's also a workaround for the problem just follow the hyperlink.

like image 106
Denys Wessels Avatar answered Sep 29 '22 23:09

Denys Wessels


So I ended up processing the loaded document in ThisAddIn_Startup. If the Path of the document is an empty string, then we know that the document is new and has never been saved on the local machine. Else, I know it is saved (including in the temp directory) and I process it as an existing document.

private void ThisAddIn_Startup(object sender, System.EventArgs a)
{
  try
  {
    Word.Document doc = this.Application.ActiveDocument;
    if (String.IsNullOrWhiteSpace(doc.Path))
    {
      ProcessNewDocument(doc);
    }
    else
    {
      ProcessDocumentOpen(doc);
    }
  }
  catch (COMException e)
  {
    log.Debug("No document loaded with word.");
  }

  // These can be set anywhere in the method, because they are not fired for documents loaded when Word is initialized.
  ((MSWord.ApplicationEvents4_Event)this.Application).NewDocument +=
    new MSWord.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
  this.Application.DocumentOpen +=
    new MSWord.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
}

As my comment for Deni's answer states: Setting the DocumentOpen event handler in ThisAddIn.Desiger.cs's Initialize() method worked for existing documents, but NewDocument is not called for a new document initialized when Word opens, so this solution did not work. So I left the setting of DocumentOpen and NewDocument in the ThisAddIn_Startup event, else DocumentOpen will be fired when a document loads with Word as well.

like image 29
Joe W Avatar answered Sep 29 '22 23:09

Joe W