6

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.

sdds
  • 2,021
  • 2
  • 25
  • 33
  • I'm struggling with something very similar. Any chance you could show some of the additional code that you used to subscribe to the ApplicationEvents4Events handlers? – Steven DAmico Mar 11 '15 at 18:21
  • @StevenDAmico I've dumped some code here: http://pastebin.com/CRZmpBg5. I cut out some product-specific code, and the comments are hastily translated to English, but otherwise it is intact. The code is from two different files. I haven't been able to find a proper solution to this question, so in my addin the missing task pane will be created if the user clicks the addin ribbon button. Also, check out my other SO question on the task pane topic: http://stackoverflow.com/questions/22560441 – sdds Mar 12 '15 at 12:29

3 Answers3

6

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.

rpurr
  • 61
  • 1
  • 3
2

So I solved this problem in my solution, although I'm not sure it will be cross applicable. Sadly, mine is in VB.Net, so there may need to be some translation.

First, I ended up not using ApplicationEvents4_Event Instead there are other built in event triggers you can use via "ThisAddIn"

  Private Sub Application_NewDocument(ByVal Doc As Word.Document) Handles Application.NewDocument
        'MsgBox("I opened something")
        myCustomTaskPane = Me.CustomTaskPanes.Add(New MyCustomTaskPaneClass, "TaskPane", Doc.ActiveWindow)
        myCustomTaskPane.Visible = True
    End Sub

Using this method I did have a similar challenge. Running winword.exe, and thereby opening a new word document, did not trigger the NewDocument event. Luckily, there was another event to use - Document change.

Private Sub Application_DocumentChange() Handles Application.DocumentChange
    'function to test if the ActiveDocument has a taskpane from my add-in, and then a function to add one        
    If Not HasMyCustomTaskPane() then AddCustomTaskPane()
End Sub

So - bottom line, regardless of if you keep using ApplicationEvents4_Event you should see if you can use the DocumentChange event. It triggers when a new word window is selected.

Steven DAmico
  • 321
  • 4
  • 11
  • Thanks. I ended up completely forgoing ApplicationEvents4_Event.NewDocument and Application.DocumentOpen and using Application_DocumentChange instead. Checking if a task pane exists for the active document was required anyway when using Application.DocumentOpen, which in some cases fired for already opened documents. – sdds Apr 02 '15 at 14:27
1

Handling task panes for more than one window in Word is fairly complicated, because of how Word loads and re-uses open windows. To do it correctly, you have to consider different actions:

  • The user takes an action to display or hide a task pane.
  • The user creates a new document.
  • The user opens an existing document.
  • The user closes an open document.

There's a tutorial that explores all the details, both in VB and C#: https://msdn.microsoft.com/en-us/library/bb264456%28v=office.12%29.aspx

I also found a similar answer on SO.

Community
  • 1
  • 1
Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111