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
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);
}