Excel BeforSave and closing excel application

Add-in Express™ Support Service
That's what is more important than anything else

Excel BeforSave and closing excel application
 
Gutek Jakub




Posts: 39
Joined: 2017-06-06
hi,

got simple issue (i hope so).

I'm listing to
WorkbookBeforeClose
and
WorkbookBeforeSave
events in Excel.

More over I do have support for
FileSave
idMSO (this is called when pressing Save icon).

everything works fine that means:
1) when user click save
2) when user click save as

I'm showing warning message to user and he can decide what to do next: cancel operation or continue. If cancel, event is cancelled and "select file" screen does not shows up.

However, in case of ALT+F4 or clicking on close button of the window i'm seeing this behavior:
1)
WorkbookBeforeClose
is called
2) normal excel information of "do you want save this" shows with Save, Don't Save, Cancel
3) when clicked on Save, Excel shows up file selection dialog
4) after name is provided and button save clicked excel calls
WorkbookBeforeSave
event
5) cancelling this event will cause that excel shutdowns and file is not saved and this is NO GO :)

Therefore, how can I and if this is even possible with Add-In Express, to show my message before select file dialog (3) shows up just as with normal save and save as operations.

I can't handle this on
WorkbookBeforeClose
as otherwise use will never be able to exit without saving (the only way to pass this is to confirm that he know what he is doing <this is my dialog>, and users don't want to see it if they do not want to save a file).

Thanks for help on this!
Posted 01 Mar, 2018 10:47:20 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
Hello Jakub,

I suggest to use WorkbookBeforeClose to get the user's response. If the response is "do not save the file", set Workbook.Saved=true and let Excel close. If the response is "I want to save the workbook", you either call Workbook.Save() or show the FileSaveAs dialog, retrieve the path and file name chosen and call Workbook.SaveAs(). In this scenario, in WorkbookBeforeClose, you should set a flag that you read in WorkbookBeforeSave to bypass other logic in that event handler.


Andrei Smolin
Add-in Express Team Leader
Posted 02 Mar, 2018 05:06:57 Top
Gutek Jakub




Posts: 39
Joined: 2017-06-06
Hi,

Yes, and this is an issue. Users don't want to see our prompt if they do not wish to save document. in word this works ok, so when you closing Word, you have default microsoft window asking "Want to save your changes to Document1?" with 3 buttons: Save, Dont Save, Cancel.

When they click Don't save word is closing. When they click save, BeforeSave event is called and our popup shows up and only if they will agree file save dialog will be showed.

In Excel this works differently. They want to have same behaviour as in word.

Therefore:
1) Default excel question "Want to save your changes..."
2) On Yes, our popup
3) Then FileSaveDialog

if I will handle BeforeClose, default question is not yet showed therefore I do not know the intention of that close (save or don't save)

If answer is: this is not possible with Add-In express and/or Excel interop - i'm fine with it :)

Cheers
Posted 02 Mar, 2018 05:17:11 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
I suggest that you imitate the Excel's "Save, Don't Save, Cancel" dialog. Show it in the WorkbookBeforeClose event if Workbook.Saved is false. If you choose this way, you need to prevent the built-in "Save, Don't Save, Cancel" dialog from being shown. To achieve this, the WorkbookBeforeClose event must end with Workbook.Saved set to true: you ether set it yourself (if the user don't want to save the file) or by calling Workbook.Save() or Workbook.SaveAs().

What you deal with is the Excel object model: Add-in Express sort of translates/re-sends these events to you.


Andrei Smolin
Add-in Express Team Leader
Posted 02 Mar, 2018 05:36:42 Top