Creating and managing custom task panes for multiple documents in a VSTO Word addin

后端 未结 3 366
温柔的废话
温柔的废话 2020-12-08 23:11

I\'m developing a Word 2007-2010 addin using VSTO in Visual Studio 2008. In my addin, I need a custom task pane for each open word document. Basically, I need to create a ta

3条回答
  •  眼角桃花
    2020-12-08 23:27

    This answer in MSDN

    Instead of reactively cleaning up orphaned task panes after you open an existing document, you can proactively clean up by calling the following method from the DocumentOpen event handler before you call CreateTaskPaneWrapper. This code loops through each of the custom task panes that belong to the add-in. If the task pane has no associated window, the code removes the task pane from the collection.

    private void RemoveOrphanedTaskPanes()
    {
        for (int i = Globals.ThisAddIn.CustomTaskPanes.Count; i > 0; i--)
        {
            CustomTaskPanes ctp = Globals.ThisAddIn.CustomTaskPanes[i - 1];
            if (ctp.Window == null)
            {
                this.CustomTaskPanes.Remove(ctp);
            }
        }
    }
    
    private void Application_DocumentOpen(Word.Document Doc)
    {
        RemoveOrphanedTaskPanes();
        CreateTaskPaneWrapper(document);
    }
    

提交回复
热议问题