Как отследить открытие и закрытие документов в приложении Microsoft Word
Как отследить открытие и закрытие документов в приложении Microsoft Word
Как отследить открытие и закрытие документов в приложении Microsoft Word Основной модуль, регистрация и вызов ... public { public declarations } fwordapp: _application; fworddoc: _document; fwordsink: twordconnection; ... procedure startwordconnection(wordapp: _application; worddoc: _document; var wordsink: twordconnection); var pointcontainer: iconnectionpointcontainer; point: iconnectionpoint; begin try // twordconnection is the com object which receives the // notifications from word. make sure to free wordsink when // you are done with it. wordsink := twordconnection.create; wordsink.wordapp := wordapp; wordsink.worddoc := worddoc; // sink with a word application olecheck(wordapp.queryinterface(iconnectionpointcontainer, pointcontainer)); if assigned(pointcontainer) then begin olecheck(pointcontainer.findconnectionpoint(applicationevents, point)); if assigned(point) then point.advise((wordsink as iunknown), wordsink.appcookie); end; // sink with a word document advise olecheck(worddoc.queryinterface(iconnectionpointcontainer, pointcontainer)); if assigned(pointcontainer) then begin olecheck(pointcontainer.findconnectionpoint(documentevents, point)); if assigned(point) then point.advise((wordsink as iunknown), wordsink.doccookie); end; except on e: exception do showmessage(e.message); end; end; procedure tmainform.btnstartclick(sender: tobject); begin fwordapp := coapplication_.create; fworddoc := fwordapp.documents.add(emptyparam, emptyparam); fwordapp.visible := true; startwordconnection( fwordapp, fworddoc, fwordsink ); end; procedure tmainform.btnexitclick(sender: tobject); begin fwordapp.quit(emptyparam, emptyparam, emptyparam); end; ¦?o?U? отслеживания линков unit connectionobject; interface uses word_tlb, dialogs; type twordconnection = class(tobject, iunknown, idispatch) protected {iunknown} function queryinterface(const iid: tguid; out obj): hresult; stdcall; function _addref: integer; stdcall; function _release: integer; stdcall; { idispatch } function getidsofnames(const iid: tguid; names: pointer; namecount, localeid: integer; dispids: pointer): hresult; stdcall; function gettypeinfo(index, localeid: integer; out typeinfo): hresult; stdcall; function gettypeinfocount(out count: integer): hresult; stdcall; function invoke(dispid: integer; const iid: tguid; localeid: integer; flags: word; var params; varresult, excepinfo, argerr: pointer): hresult; stdcall; public wordapp: _application; worddoc: _document; appcookie, doccookie: integer; end; implementation { iunknown methods } uses windows, activex, main; procedure logcomment(comment: string); begin form1.memo1.lines.add(comment); end; function twordconnection._addref: integer; begin result := 2; end; function twordconnection._release: integer; begin result := 1; end; function twordconnection.queryinterface(const iid: tguid; out obj): hresult; begin result := e_nointerface; pointer(obj) := nil; if (getinterface (iid, obj)) then result := s_ok; if not succeeded (result) then if (isequaliid(iid, documentevents) or isequaliid(iid, applicationevents)) then if (getinterface(idispatch, obj)) then result := s_ok; end; { idispatch methods } function twordconnection.getidsofnames(const iid: tguid; names: pointer; namecount, localeid: integer; dispids: pointer): hresult; begin result := e_notimpl; end; function twordconnection.gettypeinfo(index, localeid: integer; out typeinfo): hresult; begin pointer(typeinfo) := nil; result := e_notimpl; end; function twordconnection.gettypeinfocount(out count: integer): hresult; begin count := 0; result := e_notimpl; end; function twordconnection.invoke(dispid: integer; const iid: tguid; localeid: integer; flags: word; var params; varresult, excepinfo, argerr: pointer): hresult; begin //this is the entry point for word event sinking result := s_ok; case dispid of 1: ; // startup 2: showmessage( 'quit' ); // quit 3: ; // document change 4: ; // new document 5: ; // open document 6: showmessage( 'close' ); // close document else result := e_invalidarg; end; end; end. Боpисов Олег hиколаевич