我如何在 Delphi XE7 中的变体点之后获得函数和 属性 提案?

How can I get function and property proposals after the dot of a variant in 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;

谁能告诉我为什么我在输入 "word." 或 "doc." 后没有得到任何功能或 属性 提议,我该如何解决?

提前致谢,

莉亚

当您通过 CreateOleObject 函数使用 OLE 自动化时,Delphi 没有关于对象方法或属性的数据,因此它无法调用代码完成。当您的代码编译时,它只会创建 IDispatch 接口调用调用,因此从技术上讲,您可以编写任何不存在的方法名称(例如 doc.SomeSillyNonExistentFunction),并且它会成功编译并仅在运行时失败。这称为 后期绑定 - 函数存在和参数检查是在运行时而不是编译时执行的。要访问类型数据,您必须导入办公类型库,或使用 Delphi 办公自动化 VCL 组件,这几乎是一样的。

更新: 要安装 office VCL 组件,请使用菜单 Components->Install package 并启用程序包 Microsoft Office sample Automation server Wrapper components。它将在组件面板中添加一个新选项卡。如果没有这样的包(我有 XE1 但无法检查)使用菜单 Components->Import component、select Import a Type Library 选项,然后在列表中找到 Office Word/Excel/etc 类型库。