我正要学习将MsWord与Delphi连接。我编写的小型程序非常有效,但是在对象出现点之后我没有得到建议。
我的代码(我复制了我认为可能会有帮助的所有内容):
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, ComObj, ComCtrls, Vcl.StdCtrls;
var
Form1: TForm1;
word, doc : Variant;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
try
word := CreateOleObject('Word.Application');
word.Visible := true;
doc := word.Documents.Add();
word.Selection.Font.Name := ('Arial');
doc.Range.Text := 'The answer is 42.';
//doc.Save;
except
ShowMessage('Microsoft Word couldn''t start');
end;
end;
谁能告诉我为什么输入“ word”后却没有任何功能或属性建议。或“文档”。以及我该如何解决?
提前致谢,
莉亚
当您通过该CreateOleObject
函数使用OLE Automation时,Delphi没有有关对象方法或属性的数据,因此它无法调用代码完成。在代码编译时,它仅创建IDispatch
接口调用,因此从技术上讲,您可以编写任何不存在的方法名称(例如doc.SomeSillyNonExistentFunction
),它将成功编译并且仅在运行时失败。这称为后期绑定-函数存在和参数检查是在运行时而不是编译时执行的。要访问类型数据,您必须导入Office类型库,或使用Delphi Office自动化VCL
组件,这几乎是相同的。
更新:要安装Office VCL组件,请使用菜单Components->Install package
并启用软件包Microsoft Office sample Automation server Wrapper components
。它将在组件面板中添加一个新选项卡。如果没有这样的软件包(我有XE1并且无法检查)Components->Import component
,请使用menu ,选择Import a Type Library
option,然后在列表中找到Office Word / Excel / etc类型库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句