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:
- The user can simply close the actual document without closing Word and the Exited event will never fire.
- If Word is already open when 开发者_如何学Cthey click the link to open the document, the Process.Start method returns null.
- 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.
Work with Microsoft.Office.Interop.Word
(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
精彩评论