creulcat creulcat - 1 month ago 12
C# Question

DocumentBeforeSaveEventHandler event raised when save has been dismissed

Whilst writing an add-in for office, I've had to define an eventhandler for the

DocumentBeforeSave
event.

This handler works fine except for one exception.
This exception comes from the following:


  1. I open a word document and edit some text.

  2. I click on the close button.

  3. Word asks me if i want to
    save
    ,
    don't save
    or
    cancel
    .

  4. I choose
    don't save
    .



Following these steps, office still forces me in the DocumentBeforeSave event. Without a way to determine if the user intended to cancel his save or not.

Is there a way to check if the user chose to save or not, and if so how?
So far MSDN and other sources came up empty.

Note: In office 2013 and up, this should be checked by
Doc.IsInAutosave
, sadly this function doesn't exist in office 2007 and office 2010.

Answer

From what I assume based on your question you would like to run some piece of code only in the case that a document actually is saved.

A common solution to this is to intercept the actual Save command instead of registering an event handler for the DocumentBeforeSave event (which, as you describe, is also fired if the actual save operation never takes place).

How this is done is described in my answer here. Note that you would have to define the custom save command in your Ribbon XML, and then place the code that should run during save in the corresponding Ribbon callback function.