Configure IAdviseSink to Detect MSWord Document Events

Here is the code where I am trying to configure the interface IAdviseSinkimplemented in my class TForm1to detect some events of a newly created MSWord document. I have no errors while running the code, but I can’t catch any event by saving the document or closing it. How to install IAdviseSinkfor MSWord document?


    var
      Form1 : TForm1;
      doc_ole_obj : IOleObject;
      word : IDispatch;
      Connection: LongInt;

    implementation

     //------------ Setup IAdviseSink
    procedure TForm1.Setup;
    begin
     word := CreateOleObject('Word.Application');
     OleVariant(word).Visible := True;

     IUnknown(OleVariant(word).Documents.Open('file.doc')).QueryInterface(IOleObject,doc_ole_obj);
     doc_ole_obj.Advise(IAdviseSink(Self), Connection);
    end;

     //------------- catch Sink events
    procedure TForm1.OnSave;
    begin
      Caption := 'saved at ' + TimeToStr(Now);
    end;

+5
source share
2 answers

Edit:

Ignore this answer. Leaving it for educational purposes.


Why cast IAdviseSink(Self)?

If you declared an interface in the declaration of the Form class:

TForm1 = class(TForm, IAdviseSink)
   ...
end;

You do not need.

, ( , ), , . , , IAdviseSink , OnSave.


, :

Sertac " ". , IAdviseSink, ,

doc_ole_obj.Advise(IAdviseSink(Self), Connection);

[DCC Error] Unit1.pas(41): E2010 : "IAdviseSink" "TForm1"

IAdviseSink(Self), , , TForm(SomePointer), SomePointer TForm. , , , , . . - .

0

, ...

- , Advise -thing ? TWordDocument Word2000 -unit, Delphi (, , ConnectTo() )? Document OnSave ( , TLB 2000 , , , Office). Application BeforeDocumentSave -event, ...

0

All Articles