简体   繁体   中英

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 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 document by default as user should have the control over this.

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 actions from there and not rely on Word's built in 'do you want to save'.

check if document needs saving

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM