Macro to add a hyperlink to a mail message

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:="", _
End Sub

7 Responses to “Macro to add a hyperlink to a mail message”

  1. Alister Scott Says:

    Hi There,

    You can put sourcecode in tags like so

    and it will display nicely on wordpress.


  2. Alister Scott Says:

    I wasn’t expecting that, it even works in comments.
    Here is a link to how to do it.

  3. splintor Says:

    @Alister: Thank you so much. I updated the post.

  4. NVP Says:

    Awesome….. thanx for the same….

  5. sabrinahewa Says:

    this really works!! Are you looking to paste whats in your clipboard to anywhere in your email body as a hyperlink? HERES THE SOLUTION!!

  6. Michael Dew Says:

    I can get the code to run, and turn text into a hyperlink using the link address was on the clip board, but unless I right click and the click “Edit hyperlink”, and then simply click “ok” the link does not work i.e. I have to open up and then click “OK” to the Edit hyperlink dialog box before sending the email or else the link will not work – does anyone have a fix for this? Any help much appreciated.

  7. Michael Dew Says:

    Sorry, I should have said, the problem referred to in the comment above only occurs when the link is to a document path on C:, or the server, and this problem does not occur when the link is to a website URL. Thanks again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: