5

we need for our school project a way to start a word instance and track if the document was closed. The COM api from word doens't have a event for this, are there any other ways to do this?

Currently we're using the COM api from word, but everything else would be fine. We're programing in C#.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
MBulli
  • 1,659
  • 2
  • 24
  • 36
  • Would this help? http://stackoverflow.com/questions/4329521/how-to-handle-word-close-event-from-winforms-application – Koen Feb 09 '11 at 11:39
  • 1
    We tried this too, but we noticed if there was already a word proc our new proc merged into the running. We also hosting a preview so there is always a word process. – MBulli Feb 09 '11 at 11:59

2 Answers2

2

If your using the Microsoft.Office.Interop.Word library there is an event you can subscribe too:

Microsoft.Office.Interop.Word.Application wordApp =
    new Microsoft.Office.Interop.Word.Application();
wordApp.DocumentBeforeClose +=
    new ApplicationEvents4_DocumentBeforeCloseEventHandler(
        wordApp_DocumentBeforeClose);

...

private void wordApp_DocumentBeforeClose(Document Doc, ref bool Cancel)
{
    // Do your thing
}

Edit:

To take care of the file lock ==> take a look at this post. As you can see there are a few things done in the DocumentBeforeClose:

  1. Check if the document is saved. If not ==> ask where to save it and do it yourself.
  2. Close the document yourself
  3. Close Word

After these things are taken care of, you can do your stuff. The lock should be released.

Community
  • 1
  • 1
Koen
  • 2,501
  • 1
  • 32
  • 43
  • But we need a event for AfterClosed, we did it for BeforeSaved with an async while loop checkin doc.Saved == true but we found nothing like that for BeforeClose – MBulli Feb 10 '11 at 07:58
  • And what is it that you need to do once you know that the document is closed? – Koen Feb 10 '11 at 10:04
  • Read all bytes of the doc and put it in a db. The problem is that word holds a file lock, while it's open. – MBulli Feb 10 '11 at 10:10
2

Use the winword process to check if process running on particual file is exited or not.

If you dont know the process of newly started instance use this or simply hook exited event of process.

Get all word processes

var info = Process.GetProcessesByName("winword").FirstOrDefault();

Identify the process of particular file using Process.MainWindowTitle

Hook exited event of the process Process.Exited

hungryMind
  • 6,931
  • 4
  • 29
  • 45
  • Okay this should work, but what if the user creates a new document in word? – MBulli Feb 11 '11 at 07:09
  • does not matter who created process. You will always get the process using GetProcessByName. If you want to monitor any specific instance of word, u can use MainWindowTitle. – hungryMind Feb 11 '11 at 08:02
  • But if the user creates a new document the MainWindowTitle is the titel of the new doc, but I don't know if my document is still open. Word also merges process so if I already have a running instance of word and start a new one, the new one will exit instantly, so the 'Process.Exited' event doesn't work to – MBulli Feb 11 '11 at 09:58