用于捕获MSWord文档事件的IAdviseSink设置 [英] IAdviseSink setup for catching MSWord document events
问题描述
这里是我试图在我的 TForm1
类中设置 IAdviseSink
接口的代码,用于捕获一些事件新创建的MSWord文档。代码运行时我没有任何错误,但是我无法捕获任何事件,同时保存文档或关闭它。
如何正确设置MSWord文档的 IAdviseSink
Here is the code where I try to setup IAdviseSink
interface implemented in my TForm1
class for catching some events of the newly created MSWord document. I have not any errors while the code runs, but I can't catch any event, while save the document or close it.
How-to setup IAdviseSink
for MSWord document correctly?
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;
推荐答案
不完全是一个答案, ..
Not exactly an answer, but...
是否有一个特别的原因,为什么要重新发明轮子,并完成整个建议
?为什么不使用Delphi捆绑的 Word2000
-unit中的 TWordDocument
包装器类(您将拥有在这种情况下,要调用 ConnectTo()
并分配你的事件处理程序)?但是, Document
对象没有一个 OnSave
事件(至少在2000版本的TLB中,如果您希望您的应用程序与多个版本的Office兼容,则通常是一个很好的共同特征)。 应用程序
对象确实有一个 BeforeDocumentSave
-event,虽然...
Is there a particular reason why you want to reinvent the wheel and do the whole Advise
-thing yourself? Why not just use the TWordDocument
wrapper class from the Word2000
-unit that comes bundled with Delphi (all you'd have to do in that case would be to call ConnectTo()
and assign your event handlers)? However, the Document
object doesn't have an OnSave
event (at least not in the 2000 version of the TLB, which is generally a good common denominator if you want your app to be compatible with multiple versions of Office). The Application
object does have a BeforeDocumentSave
-event, though...
这篇关于用于捕获MSWord文档事件的IAdviseSink设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!