WinForms - 处理Word文档关闭事件

发布于 2024-10-05 15:54:54 字数 690 浏览 1 评论 0原文

我有一个 WinForms 应用程序,在其中提供 Word 文档的版本化链接。当用户单击链接时,它会打开 Word 文档。我的要求是当他们打开 Word 文档、执行更改、保存然后关闭文档时,检测是否已进行更改并将新版本的副本上传到数据库。

我目前已完成以下操作:

ProcessStartInfo psi = new ProcessStartInfo(FilePath) { UseShellExecute = true };
Process process = Process.Start(psi);
process.EnableRaisingEvents = true;
process.Exited += process_Exited;

这看起来应该运行良好,但是,我遇到了一些问题:

  1. 用户可以简单地关闭实际文档而不关闭 Word,并且 Exited 事件永远不会触发。
  2. 如果单击链接打开文档时 Word 已打开,则 Process.Start 方法将返回 null。
  3. 我的用户可以关闭 WinForms 应用程序,而 Word 文档可能仍保持打开状态。这可能会出现问题,因为用户可能在关闭应用程序后进行更改,并且这些更改不会保留到数据库中。

为了达到我想要的结果,我还有其他选择吗?有没有人做过类似的事情并且可以提供任何指导来解决我的问题?

预先感谢您的任何帮助。

I have a WinForms application where I provide versioned links to Word documents. When the user clicks on the links, it opens the Word document. My requirements are when they open the Word document, perform changes, save, and then close the document, to detect that changes have been made and upload a copy with a new version to the database.

I have currently done the following:

ProcessStartInfo psi = new ProcessStartInfo(FilePath) { UseShellExecute = true };
Process process = Process.Start(psi);
process.EnableRaisingEvents = true;
process.Exited += process_Exited;

This seems like it should work well, however, I ran into a few problems:

  1. The user can simply close the actual document without closing Word and the Exited event will never fire.
  2. If Word is already open when they click the link to open the document, the Process.Start method returns null.
  3. My user can close the WinForms application and the Word document may still be left open. This can be problematic because the user may make changes after closing the app and the changes will not be persisted to the database.

Are there any other options I have to achieve my desired results? Has anyone done anything similar and can provide any direction to resolve my issues?

Thanks in advance for any assistance.

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

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

发布评论

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

评论(3

找回味觉 2024-10-12 15:54:54
  1. Microsoft 合作。 Office.Interop.Word

  2. (如果您的文档存储在中央位置)尝试:FileSystemWatcher

  1. Work with Microsoft.Office.Interop.Word

  2. (If your doc is stored in a central location) try: FileSystemWatcher

千纸鹤 2024-10-12 15:54:54

您可以为 Word 编写一个 .NET 插件。
这将使您能够更好地控制 Word 中的事件。

You could write a .NET plug-in for Word.
That should give you much more control over events in Word.

小嗷兮 2024-10-12 15:54:54

为了处理“如果他们在关闭文档之前关闭我的应用程序问题”,

如果用户尝试在打开 Word 文档时关闭 Windorm 应用程序:

想法 1:
1.隐藏winform应用程序
2.当文档关闭时,然后退出winform应用程序。

想法2:
1. 显示一条消息,指示他们必须在关闭应用程序之前关闭 Word 文档。

我必须更多地了解该软件的目的是什么,以便做出更好的解决方案

To deal with the "if they close my app before they close the document issue",

IF the user tried to close the windorm app while the word document it open:

Idea 1:
1. Hide the winform app
2. When the document is closed, then exit the winform app.

Idea 2:
1. Show a message that says that they must close the word document prior to closing the application.

I would have to know more about what the purpose of the software is to make a better soultion

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