2

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.

creulcat
  • 276
  • 1
  • 17

1 Answers1

1

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.

Community
  • 1
  • 1
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • Your assumption is correct, this does solve the problem I have. Thank you very much. I never knew this was even possible. – creulcat Oct 25 '16 at 13:13
  • As this did solve some intentional problems, this method does not seem to catch the userstory of closing a document and getting prompt with the question if I would like to save my changes. – creulcat Oct 25 '16 at 13:24
  • 1
    @creulcat: If you are lucky you might get this scenario working by overwriting the respective commands (you could try e.g. `FileClose`, `FileCloseAll`, `FileCloseOrExit`, `WindowClose` or anything else that looks like it's related to closing the document). It certainly will not be simple to cover all possible scenarios... – Dirk Vollmar Oct 25 '16 at 14:27