简体   繁体   English

C# 尝试关闭未保存的Word文档时,如何知道保存提示对话框中的取消按钮是否被点击

[英]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.我正在处理 C# 字应用程序。 Currently I am using DocumentBeforeClose event for cleaning activities, disposing the objects when user tries to close the particular word document (Of multiple documents).目前我正在使用 DocumentBeforeClose 事件进行清理活动,当用户试图关闭特定的 word 文档(多个文档)时处理对象。 The problem I am facing is that when user makes some changes to word document and he tries to close the document without saving.我面临的问题是,当用户对 word 文档进行一些更改时,他试图在不保存的情况下关闭文档。 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?".然后 DocumentBeforeClose 事件被触发并执行我的逻辑,但之后对话框消息框出现“你想保存文档吗?”。 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.如果用户单击“保存”或“不保存”选项,则关闭 word 文档,但是当用户单击“取消”按钮或关闭对话框时,我们仍会在 word 文档中,但我会处理与该文档相关的对象因为事件(DocumentBeforClose)已经被触发并且用户之后无法使用文档。 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.我尝试收听 window 消息,例如 WM_Cancel,但它们对我不起作用。 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.如果我在 DocumentBeforeClose 事件中保存文档,它正在工作,但我不想默认保存文档,因为用户应该对此有控制权。

You can check if the if the document needs saving or not by checking the document.saved property.您可以通过检查 document.saved 属性来检查文档是否需要保存。 Then you can control the logic and user actions from there and not rely on Word's built in 'do you want to save'.然后你可以从那里控制逻辑和用户操作,而不是依赖 Word 的内置“你想保存吗”。

check if document needs saving 检查文档是否需要保存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道何时在C#中单击按钮 - How to know when a button is clicked in C# 如果单击保存文件对话框上的取消按钮怎么办? - How to do something if cancel button on save file dialog was clicked? 在C#.net中单击取消时如何关闭所有数据库连接 - How to close all db connection when clicked cancel in c# .net 在c#中使用word为什么总是在关闭时显示“另存为”对话框 - using word in c# why always show 'save as' dialog when close 如何通过Selenium和C#根据HTML在模态对话框中单击文本为“关闭”的按钮 - How to click on the button with text as Close within a modal dialog box as per the html through Selenium and C# 如何知道何时在form1中单击了按钮但在form2中知道C# - How to know when a button is clicked in form1 but know in form2 C# 另存为对话框C# - Save As Dialog Box C# 如何在发生冲突时保存使用 C# 创建的 excel 文件时禁用提示对话框? - How to disable the prompt dialog when I save the excel file created using C# while conflict occurs? C#保存Word文档而没有提示给出“文档在其他地方打开错误” - C# save word document without prompt give 'document is opened elsewhere error' 尝试在C#应用程序中关闭活动的Word文档时出错 - Error when trying to close an active Word document in c# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM