【发布时间】:2016-04-01 16:43:28
【问题描述】:
我一直在尝试使用这个问题的答案中显示的技术
Detect when the active element in a TWebBrowser document changes
实现 MS Word 的自动化事件的 DIY 版本。
下面是我的应用程序的更完整摘录,您可以从中看到 在这些方法中声明变量:
procedure TForm1.StartWord;
var
IU : IUnknown;
begin
IU := CreateComObject(Class_WordApplication);
App := IU as WordApplication;
App.Visible := True;
IEvt := TEventObject.Create(DocumentOpen);
end;
procedure TForm1.OpenDocument;
var
CPC : IConnectionPointContainer;
CP : IConnectionPoint;
Res : Integer;
MSWord : OleVariant;
begin
Cookie := -1;
CPC := App as IConnectionPointContainer;
Res := CPC.FindConnectionPoint(DIID_ApplicationEvents2, CP);
Res := CP.Advise(IEvt, Cookie);
MSWord := App;
WordDoc:= MSWord.Documents.Open('C:\Docs\Test.Docx');
end;
StartWord 例程运行良好。问题出在OpenDocument。这
Res := CP.Advise(IEvt, Cookie); 返回的 Res 的值是 $80040200
这在 Windows.Pas 和谷歌搜索“ole error 80040200”中的 HResult 状态代码中不存在
返回一些涉及从 Delphi 设置 Ado 事件的点击,但没有
显然相关。
反正这样的结果就是EventObject的Invoke方法永远不会 调用,所以我没有收到有关 WordApplication 事件的通知。
那么,我的问题是这个错误 $80040200 意味着什么和/或如何避免它?
Fwiw,我也尝试使用此代码连接到 ApplicationEvents2 接口
procedure TForm1.OpenDocument2;
var
MSWord : OleVariant;
II : IInterface;
begin
II := APP as IInterface;
InterfaceConnect(II, IEvt.EventIID, IEvt as IUnknown, Cookie);
MSWord := App;
WordDoc:= MSWord.Documents.Open('C:\Docs\Test.Docx');
end;
执行起来毫无怨言,但 EventObject 的 Invoke 方法永远不会 调用。
如果我将 TWordApplication 拖放到新应用程序的空白表单上,事件
像OnDocumentOpen 工作正常。我提到这一点是因为它似乎证实了
Delphi 和 MS Word (2007) 在我的机器上正确设置。
代码:
uses
... Word2000 ...
TForm1 = class(TForm)
btnStart: TButton;
btnOpenDoc: TButton;
procedure FormCreate(Sender: TObject);
procedure btnOpenDocClick(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure WordApplication1DocumentOpen(ASender: TObject; const Doc: _Document);
private
procedure DocumentOpen(Sender : TObject; DispID : Integer; var Params);
procedure StartWord; // see above for implementation
procedure OpenDocument; // --"--
procedure OpenDocument2; // --"--
public
WordDoc: OleVariant;
IEvt : TEventObject; // see linked question
Cookie : Integer;
App : WordApplication;
[...]
procedure TForm1.WordApplication1DocumentOpen(ASender: TObject; const Doc:
_Document);
begin
//
end;
我可以发布一个 MCVE,但它主要是早期答案中的代码。
【问题讨论】:
-
Crikey,我的那个答案又回来困扰我了。我看看能不能重现你的80040200。稍后...
标签: delphi ms-word delphi-7 delphi-10-seattle