Vb.net 以编程方式将超链接添加到字符串

Vb.net 以编程方式将超链接添加到字符串,vb.net,ms-word,office-interop,Vb.net,Ms Word,Office Interop,我正在尝试使用Word Interop在VB.NET中自动创建电子邮件签名 除了尝试以编程方式添加超链接(例如,电子邮件地址)之外,一切都很好-将电子邮件写入并期望其他端邮件客户端/浏览器将其转换都很好,但我希望自己指定链接(对于社交链接、我们的隐私政策等) 以下是我正在使用的代码的相关部分: Imports Word = Microsoft.Office.Interop.Word Dim objWord As Word.Application = CreateObject("Word.App

我正在尝试使用Word Interop在VB.NET中自动创建电子邮件签名

除了尝试以编程方式添加超链接(例如,电子邮件地址)之外,一切都很好-将电子邮件写入并期望其他端邮件客户端/浏览器将其转换都很好,但我希望自己指定链接(对于社交链接、我们的隐私政策等)

以下是我正在使用的代码的相关部分:

Imports Word = Microsoft.Office.Interop.Word

Dim objWord As Word.Application = CreateObject("Word.Application")

objWord.Visible = False

Dim objDoc = objWord.Documents.Add()
Dim objSelection = objWord.Selection
Dim objEmailOptions = objWord.EmailOptions
Dim objSignatureObject = objEmailOptions.EmailSignature
Dim objSignatureEntries = objSignatureObject.EmailSignatureEntries

Try
    If Len(strEmail) > 0 Then
        objSelection.TypeText(strEmail)
    End If

Catch ex As Exception
    Debug.Print(ex.Message)
End Try
在这个例子中,我认为我应该使用以下内容来代替
objSelection.TypeText(strEmail)
is

If Len(strEmail) > 0 Then
    Dim objEmailRange = objSelection.Range.Start()
    objSelection.Hyperlinks.Add(objEmailRange, strEmail, , , strEmail)    
End If
…但是,这会引发COM异常

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Signature_v2.exe
Command failed

我是否正确定义了范围起始(基于)?我做错了什么?

超链接不是实际文本;这只是对已经存在的文本的修饰。您必须将范围、选择或文档交给
超链接的第一个参数(“锚点”)。Add()

例如:

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="http:\\www.microsoft.com"

向现有文本元素添加超链接,是。我将查看是否可以找到一个示例。使用此代码解决的问题-
objSelection.Hyperlinks.Add(锚定:=objSelection.Range,地址:=strEmail,TextToDisplay:=strEmail)
。谢谢