Today I wrote a mail to someone and wanted to add several links. It always annoyed me that Office’s Add Hyperlink is so bloated it takes some time to open, and even more time to switch back to it from another application, where all I wanted was to make the selected text link to the URL I had in my clipboard. So I set out to write a macro that did it.
Since Outlook has no macro recorder, I started searching Outlook’s Object Browser and discovered that there is no method to add a hyperlink or to get the content of the clipboard. So I borrowed the “Getting clipboard content” functionality from a VBS script I already had, and I turned to Word to record a macro that adds a hyperlink to the selected text. Once I had this Word macro, I found out I can use ActiveInspector.WordEditor to issue the Word macro I created.
The final macro result is this:
Public Sub AddLinkFromClipboard() If ActiveInspector.EditorType = olEditorText Then MsgBox "Can't add links to textual mail" Exit Sub End If Dim objHTM As Object Dim ClipboardData As String Dim doc As Object Dim sel As Object Set objHTM = CreateObject("htmlfile") ClipboardData = objHTM.ParentWindow.ClipboardData.GetData("text") Set doc = ActiveInspector.WordEditor Set sel = doc.Application.Selection doc.Hyperlinks.Add Anchor:=sel.Range, _ Address:=ClipboardData, _ SubAddress:="", _ ScreenTip:="", _ TextToDisplay:=sel.Text End Sub