在 Outlook 正文中插入超链接

Insert Hyperlink in Outlook body

所以我正在尝试创建一个代码来加快在 Outlook 中插入超链接的速度。

我正在尝试拥有它,这样如果我已经复制了一个路径,我可以直接进入并键入 Ctrl W,它将在此处插入单词的超链接。我对代码的尝试是:

Sub InsertHyperlink()
'
'
'
On Error Resume Next
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
    "U:\plot.log", _
    SubAddress:="", ScreenTip:="", TextToDisplay:="here"
End Sub

我有关于如何更新我的代码以便在 Outlook 中工作的问题(我在 Word 中编程)并且 "U:\plot.log" 实际上是复制的路径(不是复制的路径当我录制宏)。

有人有什么建议吗?

设置对 Word 对象库的引用

Tools > References > add Word object Library

Option Explicit
Sub Add_Hyperlinks()
    Dim olNameSpace As Outlook.NameSpace
    Dim wDoc As Word.Document
    Dim rngSel As Word.Selection

    If Application.ActiveInspector.EditorType = olEditorWord Then
        Set wDoc = Application.ActiveInspector.WordEditor ' use WordEditor
        Set olNameSpace = Application.Session
        Set rngSel = wDoc.Windows(1).Selection ' Current selection

        wDoc.Hyperlinks.Add rngSel.Range, _
        Address:="U:\plot.log", TextToDisplay:="Here is the link"
    End If

    Set wDoc = Nothing
    Set olNameSpace = Nothing

End Sub

非常感谢您的帮助,非常感谢!因此,我对您的代码做了一些改动,以尝试让它粘贴剪贴板上的任何内容。

我的新代码如下。我需要添加任何错误捕获吗?另外,explicit 选项究竟有什么作用?

Option Explicit
Sub Add_Hyperlinks()
   Dim olNameSpace As Outlook.NameSpace
   Dim wDoc As Word.Document
   Dim rngSel As Word.Selection
   Dim DataObj As MSForms.DataObject
   Set DataObj = New MSForms.DataObject
   DataObj.GetFromClipboard
If Application.ActiveInspector.EditorType = olEditorWord Then
    Set wDoc = Application.ActiveInspector.WordEditor ' use WordEditor
    Set olNameSpace = Application.Session
    Set rngSel = wDoc.Windows(1).Selection ' Current selection
    wDoc.Hyperlinks.Add rngSel.Range, _
    Address:=DataObj.GetText(1), TextToDisplay:="here"
End If
Set wDoc = Nothing
Set olNameSpace = Nothing
End Sub