I have a Microsoft Word Add-in developed in C#. When a user tries to close a document with unsaved changes, the Save dialog box is shown. Is there a way to capture if the user clicks cancel on the Save dialog?
The DocumentBeforeClose event is annoyingly fired before the Save dialog is shown and it appears an event isn't fired encapsulating the Save Dialog event.
Why I need this: If the user closes the last document, I need to do some clean up on my Add-in tool bar. However, if I do the clean up in the DocumentBeforeClose event and the user clicks cancel, the Add-in tool bar state is incorrect.
Edit/Update:
As Rob Allen suggested, I am now using the ShutDown event to do cleanup of my parent object and the BeforeClose event for pre-close validation of my parent object. My validation needs the document to be saved and tries to do that in a method called by BeforeClose. If the document has never been saved before, the Save As dialog appears. If the user clicks Cancel a COMException is thrown and I catch that. Then the Save/Don't Save/Cancel dialog appears. If the user clicks Don't Save, the ShutDown event is not fired. If the user saves from the Save As dialog, saves from the second dialog, or hits cancel on the 2nd dialog, everything functions correctly.
private void WordDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
this.WordDocument.Save();
}
catch (System.Runtime.InteropServices.COMException a)
{
log.Error(a.Message);
}
}
private void WordDocument_Shutdown(Object sender, EventArgs e)
{
// Parent cleanup.
}