detecting when a user opens a word document and when he types in it (in c#)

╄→尐↘猪︶ㄣ 提交于 2020-01-04 02:02:36

问题


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.



来源:https://stackoverflow.com/questions/11072829/detecting-when-a-user-opens-a-word-document-and-when-he-types-in-it-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!