none
Word 2013 fires 'BeforeSave' event to addin during document close even though 'Don't Save' was selected RRS feed

  • Question

  • I've got a shared Add-in written in C# that runs in Word and various other office applications. The Add-in handles the BeforeSave event which does various processing which may involve a message box being shown, or it may cancel the Save event

    With Word 2013, if I create a new Word document, enter some content and then attempt to close the document, Word asks me if I want to save changes. Even if I select 'Don't Save', my Add-in receives a BeforeSave event which causes my processing to run - this didn't happen in previous versions of Word. 

    Has anyone else seen this problem, or know how to work around it?

    Friday, February 15, 2013 10:21 AM

Answers

  • Hi Steve / All

    It turns out that this behavior is not a bug - it's by design. We're seeing it because Word now does an AutoSave when you close a document, even if the user chooses not to save changes. Word 2013 provides a way to get your changes back if you accidentally close a document without saving - and this is the mechanism it uses.

    In order to test whether this is the reason BeforeSave triggers, you can use the Document.IsInAutoSave property. This property was introduced in Word 2013:
      http://msdn.microsoft.com/en-us/library/office/jj230857.aspx

    So:
      if (!Doc.IsInAutoSave)
      { //Perform actions }


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Steve D. _ Tuesday, March 19, 2013 9:52 AM
    Sunday, March 17, 2013 7:11 AM
    Moderator

All replies

  • Hi Steve,

    Thanks for posting in the MSDN Forum.

    I will involve some experts into your thread to see whether they can help you out. There might be some time delay, appreciate for your patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 18, 2013 1:55 AM
    Moderator
  • Thanks Tom - the issue can easily be seen by creating a VSTO Word Add-In with Visual Studio 2010 and adding in a handler for BeforeSave - if you create a new document, or open an existing document, make a change to the content, close the document and say no to Save Changes prompt, then the BeforeSave event still fires

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
            }

            void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                MessageBox.Show(string.Format("DocumentBeforeSave event fired\n\nDoc.Saved: {0}\nSaveAsUI: {1}\nCancel: {2}", Doc.Saved.ToString(), SaveAsUI.ToString(), Cancel.ToString()));
            }

    Monday, February 18, 2013 9:33 AM
  • Hi Steve,

    Thanks for posting in the MSDN Forum.

    I verified the behaviour in Word 2013 as well as Word 2010. I see that the in the Word add-in DocumentBeforeSave event fires in Word 2013 even if 'Don't Save' was selected while closing the document but it does not fire the event in Word 2010 if 'Don't Save' was selected while closing the document.

    We will verify this further if this qualifies as a bug and report it.

    Regards

    Pradip

    Tuesday, February 19, 2013 7:01 PM
  • I've unmarked this as an "Answer" because it is very important that Pradip Fo follows up on the issue for Word 2013. This is a major change in behavior. We need to know whether it's a bug (in my opinion, it is) and whether/when we can expect a fix. The message thread should not be closed until we have such information.

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, March 15, 2013 6:12 PM
    Moderator
  • Hi Steve / All

    It turns out that this behavior is not a bug - it's by design. We're seeing it because Word now does an AutoSave when you close a document, even if the user chooses not to save changes. Word 2013 provides a way to get your changes back if you accidentally close a document without saving - and this is the mechanism it uses.

    In order to test whether this is the reason BeforeSave triggers, you can use the Document.IsInAutoSave property. This property was introduced in Word 2013:
      http://msdn.microsoft.com/en-us/library/office/jj230857.aspx

    So:
      if (!Doc.IsInAutoSave)
      { //Perform actions }


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Steve D. _ Tuesday, March 19, 2013 9:52 AM
    Sunday, March 17, 2013 7:11 AM
    Moderator
  • Thanks Cindy - I'd not noticed that new property, so I've added a check for it and it all works fine now. 

    Tuesday, March 19, 2013 9:54 AM
  • Hi, I see that this thread is old and answered but I have the same problem with my winforms application, however, I don't have the property "IsInAutosave" on my document object.

    What framework and what version should I use?

    I'm currently using Microsoft.Office.Interop version 14.0.0...

    Thanks,
    • Edited by matmol Tuesday, October 29, 2013 1:22 PM
    Tuesday, October 29, 2013 1:21 PM