Lea*_*Lea 1 delphi ms-word object variant delphi-xe7
我正准备学习将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;
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我为什么在输入"word"后我没有得到任何功能或财产建议.或"doc." 以及如何解决这个问题?
提前致谢,
LEA
当您通过该CreateOleObject函数使用OLE自动化时,Delphi没有关于对象方法或属性的数据,因此它无法调用代码完成.当您的代码编译时,它只是创建IDispatch接口调用调用,因此从技术上讲,您可以编写任何不存在的方法名称(例如doc.SomeSillyNonExistentFunction),它将成功编译并仅在运行时失败.这称为后期绑定 - 函数存在,并且在运行时而不是编译时执行参数检查.要访问类型数据,您必须导入office类型库,或使用Delphi办公自动化VCL组件,这几乎是一样的.
更新:
要安装Office VCL组件,请使用菜单Components->Install package和启用包Microsoft Office sample Automation server Wrapper components.它将在组件面板中添加一个新选项卡.如果没有这样的包(我有XE1但无法检查)使用菜单Components->Import component,选择Import a Type Library选项,然后在列表中找到Office Word/Excel/etc类型库.
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |