detecting when a user opens a word document and wh

2019-07-06 22:39发布

问题:

i'd like to do the following : whenever a word document is open i need to save it in a way, and then if a user starts typing in it i want to save the time the document is being edited. i'm just on the first phase, and i can't seem to manage detecting when a user opens a document. i tried using Microsoft.Office.Interop.Word, but, in this way i don't want to start word application unless the user opens a document. but, when i want to initialize a Microsoft.Office.Interop.Word.Application, it's the only way i saw possible. is there a way, by using the Microsoft.Office.Interop.Word API to detect event of opening a file by a user ?

i tried the following (obviously it doesn't work, since it's just opens a word office application)

using Word = Microsoft.Office.Interop.Word;
Word.Application oWord = new Word.Application(); 
oWord.Visible = true;
oWord.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);
...

private void oWord_DocumentChange()
{
   Console.WriteLine("DocumentChange");
}

also, i wanted to maybe use Microsoft.Office.Interop.Word.Document, but couldn't. i started developing a method of my own, but its just seems to be a waste since this api is already build. any help will be great.. thanks.

回答1:

Have you already tried creating an Application level Add-in. That add-in should have all the event handlers you need to detect the first and last change to the document.



回答2:

Maybe you could keep checking for open instances of Word every so often, and if you find one, then you use interop to get that instance.

You could probably use something like FindWindow or EnumWindows to check for instances of Word (or there might be some built in way of doing that in .Net that I can't remember right now), and then perhaps use GetObject to get the instance. This link describes GetObject vs CreateObject.