Reputation: 1890
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?
}
Upvotes: 6
Views: 4982
Reputation: 10605
I've used the Application.Documents collection in the Startup handler to find all document in existence before my add-in loads. It accomplishes the same thing your answer does, plus, if you load the add-in later on demand (rather than when Word starts) for whatever reason, enumerating the collection can get more than just the active document.
The DocumentChange() event fires whenever the ActiveDocument changes. Word only has one active document at a time. Switching between multiple open Word documents will fire the event. Also, when closing documents, the event could fire as another open document becomes active. It also fires after the last document closes, in which case, Application.ActiveDocument will be invalid.
Upvotes: 1
Reputation: 1890
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.
Upvotes: 2
Reputation: 17029
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.
Upvotes: 2