How to know whether cancel button is clicked in Save Prompt dialog box , when we try to close Unsaved Word document in C#

120

Question: How to know whether cancel button is clicked in Save Prompt dialog box , when we try to close Unsaved Word document in C#

I am working on C# word application. Currently I am using DocumentBeforeClose event for cleaning activities, disposing the objects when user tries to close the particular word document (Of multiple documents). The problem I am facing is that when user makes some changes to word document and he tries to close the document without saving. Then DocumentBeforeClose event is fired and my logic is executed, but after that Dialog message box appears with "Do you want to save the document?". If the user clicks on Save or Don't c# error Save option the word document is closed, but when user clicks on cancel button or closes the dialog box we would be still there in word document, but I would have disposed the objects related to that document as the event(DocumentBeforClose) is already fired and user can't work with document afterwards. Is there any way I could know whether the cancel button is clicked or not? I tried listening to window messages such as WM_Cancel , but they are not working for me. How can I overcome this scenario?

If I save the document in DocumentBeforeClose event it is working, but I don't want to save the c# error document by default as user should have the control over this.

Total Answers: 1

46

Answers 1: of How to know whether cancel button is clicked in Save Prompt dialog box , when we try to close Unsaved Word document in C#

You can check if the if the document needs saving or not by checking the document.saved property. Then you can control the logic and user c# error actions from there and not rely on Word’s built in ‘do you want to save’.

check if document needs saving