Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTO Word add-in - new document event not firing if Word is launched from the executable

In my add-in, I need to create a task pane for each open document. In the add-in's startup method, I subscribe to the ApplicationEvents4_Event.NewDocument and Application.DocumentOpen events, and then create a task pane for each opened document:

((ApplicationEvents4_Event)Application).NewDocument += CreateTaskPaneWrapper;
Application.DocumentOpen += CreateTaskPaneWrapper;

foreach (Document document in Application.Documents)
{
    CreateTaskPaneWrapper(document);
}

This covers cases for opening or creating a document through Word's menu, or opening an existing document file in the OS. However, if Word is already opened, launching WINWORD.EXE (or accessing it through a shortcut, which is a pretty common scenario) doesn't trigger either event, despite a new window with a new document being opened. How can I react to this scenario and create a task pane for a document created this way? I'm using VSTO 3 and Visual Studio 2008, targeting Word 2007.

like image 982
sdds Avatar asked Apr 01 '14 09:04

sdds


People also ask

How do I initialize my VSTO add-in when it is loaded?

The ThisAddIn_Startup and ThisAddIn_Shutdown event handlers. These event handlers are called when Word loads and unloads your VSTO Add-in. Use these event handlers to initialize your VSTO Add-in when it is loaded, and to clean up resources used by your VSTO Add-in when it is unloaded.

How do I create a word VSTO add-in project?

On the File menu, point to New, and then click Project. In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint. Under the expanded Office/SharePoint node, select the Office Add-ins node. In the list of project templates, select a Word VSTO Add-in project. In the Name box, type FirstWordAddIn. Click OK.

What are the VSTO add-in event handlers used for?

These event handlers are called when Word loads and unloads your VSTO Add-in. Use these event handlers to initialize your VSTO Add-in when it is loaded, and to clean up resources used by your VSTO Add-in when it is unloaded. For more information, see Events in Office projects.

What is the shutdown event for the VSTO add-in?

This event is handled by the ThisAddIn_Shutdown method in the generated code file. This event handler is the last user code to run when the VSTO Add-in is unloaded. The Shutdown event is raised only when the user disables the VSTO Add-in by using the COM Add-ins dialog box in Outlook. It is not raised when Outlook exits.


1 Answers

If Word is started, a new document is created BEFORE the Add-In loads, therefore this event can not be trapped.

If you need to work with the initially created document, just take a look at the Documents collection - if Count is greater zero, this document is the one created by Word before your Add-In was loaded.

like image 163
rpurr Avatar answered Jan 03 '23 00:01

rpurr