locked
Detect when a Word window is closed? RRS feed

  • Question

  • I'm managing Task Panes on multiple Word windows by storing their instance in a dictionary using the ActiveWindow as the key value.  This works pretty good for keeping track of them, but I need to be able to detect when a Word window is closed (and what that window's activewindow id is) so I can clear its task panes out of memory.

    Does anybody know how to tie into an event to detect a window being closed?  Also, is this a good way to keep track of Task Panes?  I don't really know what the ActiveWindow property is for, is there any chance it could change while the window is open? 
    Tuesday, December 9, 2008 3:27 PM

Answers

  • 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.
    • Proposed as answer by Andrew Wiles Thursday, December 11, 2008 6:33 AM
    • Marked as answer by Tim Li Tuesday, December 16, 2008 6:38 AM
    Tuesday, December 9, 2008 6:41 PM
    Answerer