Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何将超链接添加到OleContainer中的open word文档中_Delphi_Hyperlink_Containers_Ole - Fatal编程技术网

Delphi 如何将超链接添加到OleContainer中的open word文档中

Delphi 如何将超链接添加到OleContainer中的open word文档中,delphi,hyperlink,containers,ole,Delphi,Hyperlink,Containers,Ole,我想使用MsWord作为HTML文档的编辑器。 我打开另一张表格,上面有一些文件列表。 我希望用户选择其中一个文件 并将其作为alink添加到打开的文档中(在用户选择的位置) 我在olecontainer中创建的word中打开HTML文档。 与: 用分子框架做 开始 OleContainer1.CreateObjectFromFile(FileToEditName{myfile.html},False); OleContainer1.AutoActivate:=aaGetFocus; 分子容器1

我想使用MsWord作为HTML文档的编辑器。 我打开另一张表格,上面有一些文件列表。 我希望用户选择其中一个文件 并将其作为alink添加到打开的文档中(在用户选择的位置)

我在olecontainer中创建的word中打开HTML文档。 与: 用分子框架做 开始 OleContainer1.CreateObjectFromFile(FileToEditName{myfile.html},False); OleContainer1.AutoActivate:=aaGetFocus; 分子容器1.DoVerb(ovOpen); 分子容器1。运行; 结束

如何添加此链接,如: 添加每个链接(某些文本,超链接)。。。。
在用户选择的位置,假设您的表单上有一个包含URI的TEdit(我使用了BBC的网站)。然后,以下代码将在您的OLEContainer中的活动Word文档中向其添加超链接:

procedure TForm1.Button1Click(Sender: TObject);
begin
  OleContainer1.OleObject.ActiveDocument.Hyperlinks.Add(
  Anchor := OleContainer1.OleObject.Selection.Range,
  Address := Edit1.Text,  // contains e.g. http://www.bbc.co.uk
  TextToDisplay := 'Link'
  );
end;
其工作方式是,OleContainer1.OleObject是Word.Application的变体引用(参见Delphi附带的Word2000.Pas单元),一旦有了此引用,您就可以使用后期(或早期)绑定调用Word的自动化方法

顺便说一句,OleContainer1.OleObject.ActiveDocument.Hyperlinks.Add参数的异常语法是Delphi支持的一种特殊语法,用于在后期绑定调用中使用命名参数

更新:您在评论中说,您已尝试了上述代码,但出现错误“自动化对象不支持方法“选择”。当我把我的测试项目放在一起时,我没有在HTML和MS Word之间建立关联,所以我编写了激活Word所需的代码,并将HTML文件加载到其中。我在FormCreate事件中执行此操作:

procedure TForm1.FormCreate(Sender: TObject);
var
  V : OleVariant;
  AFileName : String;
begin
  OleContainer1.CreateObject('Word.Application', False);
  OleContainer1.Run;
  V := OleContainer1.OleObject;
  Caption := V.Name;
  V.Visible := True;
  AFileName := ExtractFilePath(Application.ExeName) + 'Hello.Html';
  V.Documents.Add(AFileName);
end;
请注意,此按钮和单击按钮1是我的项目的整个代码,它会根据您的要求插入链接。如果你得到一个不同的结果,我想这一定是因为我们读者看不到你设置的某些细节。

是的,这是有效的。 我不知道现在我们能用什么

(锚定:=。。。。
);

但是现在 word删除执行集路径并将其更改为“href=”../../../../MzIAI/Images/2019-06/12/45545_5679.Pdf“>”
并删除完整路径

您好,谢谢。我试过了。但是我在if CatalogRec.Found然后begin NewFile:=StringReplace(CatalogRec.Mz_FileName,'\','/',[rfReplaceAll,rfIgnoreCase])上得到了“自动化对象不支持的方法'选择'”;OleContainer1.OleObject.ActiveDocument.Hyperlinks.Add(锚定:=OleContainer1.OleObject.Selection.Range,地址:=NewFile,//包含例如TextToDisplay:=selectDocumentFrm.TypedHyperText);结束;现在,我将顺序更改为:OleContainer1.OleObject.Application.ActiveDocument.Hyperlinks.Add(锚定:=OleContainer1.OleObject.Application.Selection.Range,地址:=NewFile,//包含例如TextToDisplay:=selectDocumentFrm.TypedHyperText);而且它起作用了。感谢您的帮助查看“我的答案”的更新,并请说明它是否适用于您。此外,请通过单击其左侧的“勾选”图标接受此答案,以便将来的读者会看到它回答了您的问题。