Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Is there an event after a document is closed? (VSTO, C#)
 

Is there an event after a document is closed? (VSTO, C#)

Hello,

I am writing an add on for word, using VSTO 2008, c#.
During which some memory is being allocated for every open document. I want to free this memory (or release the reference) which is document dependent. For this I am looking for an event similar to Application.DocumentBeforeClose, but I am looking for something like Application.DocumentAfterClose, since if a user clicks "Cancel" I do not want to release the reference (a big no no =) ).

I have tried Application.WindowDeactivate, which occurs on the correct timing, but also on other occasions which are indistinguishable programmatically (unless I'm wrong here =) )

Can anyone help, please?

Tal Kramer
Tal Kramer  Wednesday, September 03, 2008 5:18 PM

There is a property called Saved on the Document object that is read write, you could ask the question;

If Doc.Saved = false then Do you want to save Changes?

If Yes then Cleanup Doc -> Doc.Save -> Doc.Saved= true

If the above is in the event then the solution will not prompt the user if the Saved property is suitably set.

Regards

Mike Walker  Wednesday, September 03, 2008 10:56 PM

You don't say whether you are writing a VSTO Addin or Template but the events you have mentioned for DocumentBeforeClose is the only event on the ApplicationClass that can be caught to clean up. What type of objects are you wishing to clean?

Regards

Mike Walker  Wednesday, September 03, 2008 5:50 PM
Mike Walker wrote:

You don't say whether you are writing a VSTO Addin or Template but the events you have mentioned for DocumentBeforeClose is the only event on the ApplicationClass that can be caught to clean up. What type of objects are you wishing to clean?

Regards




I am writing an addin. But I am not looking for an application class event. I am looking for any way to catch a document (or window) after it was closed (or at a point that when the document/window has already been closed, or there is no way to stop it from closing). I have a dictionary with entries for each document, elements should be removed once a document/window is closed.

Thanks for the comment, hope this clears up the issue...

Tal
Tal Kramer  Wednesday, September 03, 2008 6:08 PM

There is an event DocumentBeforeClose of the Application that you could trap to put your clean up code inplace.

Regards

Mike Walker  Wednesday, September 03, 2008 6:41 PM
Mike Walker wrote:

There is an event DocumentBeforeClose of the Application that you could trap to put your clean up code inplace.

Regards



Hi, thanks,

But there is a problem with this solution.

The problem with "DocumentBeforeClose" (as I mentioned above) is that if the document is dirty, then the event is raised before the "Do you want to save the document before leaving? Yes/No/Cancel". Therefore if I do the clean up there, and the user chooses "cancel", the cleanup has been done yet the document stays open. This is something I cannot allow.
Thanks for the answer though =)


Tal
Tal Kramer  Wednesday, September 03, 2008 7:45 PM

There is a property called Saved on the Document object that is read write, you could ask the question;

If Doc.Saved = false then Do you want to save Changes?

If Yes then Cleanup Doc -> Doc.Save -> Doc.Saved= true

If the above is in the event then the solution will not prompt the user if the Saved property is suitably set.

Regards

Mike Walker  Wednesday, September 03, 2008 10:56 PM
Mike Walker wrote:

There is a property called Saved on the Document object that is read write, you could ask the question;

If Doc.Saved = false then Do you want to save Changes?

If Yes then Cleanup Doc -> Doc.Save -> Doc.Saved= true

If the above is in the event then the solution will not prompt the user if the Saved property is suitably set.

Regards



I was trying to avoid this workaround, since this might change the look / feel of word. But if there is no other option, then thanks =).
To complete this question, is there a way (given a reference to a document / window) to check whether it is still open?

Tal
Tal Kramer  Wednesday, September 03, 2008 11:23 PM
I have just had to do the same thing. The simple solution I found was to try and retrieve a property from the Document object. If this succeeds, then the document is still open. Otherwise, it is closed.

try {
    string filename = oDoc.FullName;
    docIsOpen = true;
}
catch {
    docIsOpen = false;
}

Midi_Mick  Thursday, September 24, 2009 11:22 PM

You can use google to search for other answers

Custom Search

More Threads

• [SOS] About Selecting Text From Word 2003 Document
• Word.Application.WindowBeforeRightClick is not raised when right-clicking picture content controls
• COM ADDIN Not working in client mechins
• visual studio 2005 Excel 2003
• Invalid Parameter Error
• regarding actions pane
• Does VSTO have EnableVbaCallers property for Word 2003 too?
• How can I count Rows and Columns
• writing into the document (VSTO - C#)
• SmartTags don