开发者

WinForms - Handle Word Document Close Event

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 开发者_如何学Cthey 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.


  1. Work with Microsoft.Office.Interop.Word

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


You could write a .NET plug-in for Word. That should give you much more control over events in 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜