2
votes

I want to catch "don't save" button click on save dialog when user is trying to close Excel workbook, but there are unsaved changes.

My case is: the user is opening the excel file changing something in the file and clicking to close button, the Excel application will display "do you want to save changes" dialog and I want to handle the cases when the user will choose no or cancel. I don't want to switch the screen and show to user my own dialog in my application, the user will not be happy for that solution. If there is a way to handle this please let me know.

Here is my code:

_xlsApp = new Excel.Application();
_xlsApp.WorkbookBeforeClose += new Excel.AppEvents_WorkbookBeforeCloseEventHandler(xlsApp_WorkbookBeforeClose);
_xlsApp.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(xlsApp_WorkbookBeforeSave);
1
Do you want to save or discard (or suppress this operations) the changes or leave this decision to the user?keenthinker
leave this decision to the user and catch his action when the user will clock on don't save buttonartos
You can't handle/intercept the default dialog and if you handle the event by yourself you need to add your own functionality that mimics the default.keenthinker

1 Answers

2
votes

IMO you need to overwrite the default save/do not save changes event with a custom one.

In your before save event handler:

This way the decision is left to the user (as default, nothing changes from the user perspective), but you can still do whatever you want to do before or after the workbook is saved.

If the user closes the application excel will go first in the before close event. You have two options: - check here if there are any changes and aks the user and save the answer and use the answer later in the before save event - or skip this event - if there are any unsaved changes, excel will either way go to the before save event, where you can proceed as shown above; from MSDN If the workbook has been changed, this event occurs before the user is asked to save changes.