Document close event in Word add-in RRS feed

  • Question

  • Hi,

    I am working on developing an add-in for Microsoft Word. When the add-in is clicked, a new window form which I have developed will be opened. The window form will be opened per each document. I am storing the list of documents that are opened and forms that are opened per each document.

    Now I want to close particular windows form when the document is closed. Regarding this, I am using DocumentBeforeClose Event on the Application object. When this event is fired, I am getting the particular windows form against the word document saved in a list. But a prompt for saving the document is displayed and before displaying this prompt the DocumentBeforeClose Event is fired and the windows form is closed.

    I want my windows form to be closed only when the document is closed after the save prompt is closed after user selects one of the options "Save", "No" or "Cancel".

    Please suggest how this can be handled.

    Thanks in advance.

    Surya Praveen

    Friday, February 22, 2013 8:04 AM

All replies

  • I'm afraid you can't do that.

    with regards, Yuri

    Monday, February 25, 2013 12:58 PM
  • The Document class has a boolean value "Saved", that will return false, if the document has been changed since last save..
    When this value is false, I'm pretty sure you can always be certain, that a save dialog will pop up..

    Maybe you can use this to determine, if you should close your winform?

    But then you will have to catch another event to actually close the winform after the save is complete


    Monday, February 25, 2013 1:26 PM
  • in that event you can actually post window message for closing your window using WinFormsSynchronizationContext.Post method, but of course you will not know what option user have chosen (you could try to deduce it from state of document)
    Monday, February 25, 2013 1:44 PM
  • Hello, thanks for all your replies. I will work on verifying the saved property on Document object and closing window using WinFormsSynchronizationContextPost method and come back to you.

    Surya Praveen

    Tuesday, February 26, 2013 6:30 AM
  • Hello,

    I tried checking Saved property on document and able to close the window only if it is true.

    However I don't understand how to handle when the dialog for Save is displayed and catch an event after user takes one of the actions 'Yes', 'No' and 'Cancel'.

    As DamianD said I tried SynchronizationContext.Current.Post. But in the event DocumentBeforeClose, the object SynchronizationContext.Current has the value Nothing.

    I found an MSDN link where the Document interface has ShutDown and CloseEvent events. However this Document interface is under the namespace Microsoft.Office.Tools.Word namespace. In the current add-in the namespace is Microsoft.Office.Interop.Word. What's the difference between these two? Can I type cast the document object of one namespace to another namespace?

    In the following link, Application-level add-ins is mentioned in the namespace Microsoft.Office.Tools.Word

    But in my add-in the namespace is Microsoft.Office.Interop.Word for Application and Document interfaces. Please suggest what can I do to catch the final close event on a document

    Wednesday, February 27, 2013 1:30 PM
  • if synchronization context is null, create your own on add-in startup event, something like:

    var syncCtx = new WinFormsSynchronizationContext ();

    and use it later - as for outcome of dialog box Yes No Cancel - as i told you previosly you can only try to use heuristics to deduce what was chosen by user.

    Wednesday, February 27, 2013 1:43 PM
  • Hello Damian,

    First of all, there is no event that is raised after user selects an option in the dialog. Then how can I know the outcome of dialog box.

    What does the WinFormsSynchronizationContext do here? Does it calls the window.close method after the user takes the action Yes/No/Cancel? Or do we have to handle it from the code? Please explain me.

    Surya Praveen

    Thursday, February 28, 2013 10:34 AM
  • no, winformssynchronizationcontext is the remedy for synchronizationcontext.current being null. As for your question about dialog before saving - you cannot see ir handle it, accept that fact. All you can do is try to reason about it after it was closed and your code gets to run finally.
    Thursday, February 28, 2013 10:51 AM
  • Oh, okay. I thought that it works something like it fires after the user takes an action on the save dialog.

    So, can anyone help me whether there is an event that fires when closing the document, instead of before closing event?

    • Edited by Surya Praveen Friday, March 1, 2013 7:17 AM Sentence correction
    Thursday, February 28, 2013 12:05 PM