The technical article
Managing Custom Task Panes in Multiple Word and InfoPath Documents is the best resource I'm aware of for strategies for managing custom task panes in Word. Rather than using a Dictionary to keep track of the task panes and the document windows they are associated with, the code in this article simply loops through each CustomTaskPane object in the ThisAddIn.CustomTaskPanes collection when necessary, and removes the CustomTaskPane object from the collection if the CustomTaskPane.Window property is null (meaning that the document it was associated with is no longer open). That said, using a Dictionary as you are doing might make more sense in many scenarios, such as if you need to access controls in a specific task pane for a specific document, or if the end user might have many documents open at once.
As this article describes, you can determine when a document's window has been closed by handling the Application.DocumentBeforeClose or Document.Close events. However, if the document has unsaved changes, these events are actually raised before the end user sees the dialog that lets them cancel the close operation. If you remove the custom task pane in one of these event handlers and the end user cancels the close operation, then you will have unnecessarily removed the task pane. The article shows one way to work around this by instead removing all orphaned task panes in the Application.DocumentChange event handler.
I hope this helps,
McLean Schofield
This posting is provided "AS IS" with no warranties, and confers no rights.