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

发布于 2025-01-15 12:29:33 字数 450 浏览 2 评论 0原文

我正在开发 C# word 应用程序。目前,我正在使用 DocumentBeforeClose 事件进行清理活动,当用户尝试关闭特定的 Word 文档(多个文档)时处理对象。我面临的问题是,当用户对Word文档进行一些更改时,他尝试关闭文档而不保存。然后 DocumentBeforeClose 事件被触发并执行我的逻辑,但之后对话框消息框出现“您想保存文档吗?”。如果用户单击“保存”或“不保存”选项,Word文档将关闭,但是当用户单击“取消”按钮或关闭对话框时,我们仍会在Word文档中,但我会处理与该文档相关的对象因为事件(DocumentBeforClose)已经被触发,用户之后无法使用文档。有什么方法可以知道取消按钮是否被点击?我尝试监听诸如 WM_Cancel 之类的窗口消息,但它们对我不起作用。我怎样才能克服这种情况?

如果我将文档保存在 DocumentBeforeClose 事件中,它可以正常工作,但我不想默认保存文档,因为用户应该对此拥有控制权。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-01-22 12:29:33

您可以通过检查 document.saved 属性来检查文档是否需要保存。然后,您可以从那里控制逻辑和用户操作,而不必依赖 Word 内置的“您要保存吗”。

检查文档是否需要保存

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文