Вопрос

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.

Это было полезно?

Решение 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.

Другие советы

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.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top