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

9 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.

  8. Bartolomeu Says:

    This is exactly what I’ve been looking for. At work, when providing tracking information, I’ve been typing “(link)” and then manually selecting the word “link” and using ctrl+k to open the hyperlink and paste the hyperlink from there. It doesn’t sound like much, but I wanted a way to automate this just how you approached it.

    The only chance I made was that I added…

    sel.TypeText Text:=”(link)”
    sel.MoveLeft Unit:=wdCharacter, Count:=1
    sel.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend

    …between “Set sel = doc.Application.Selection” & “doc.Hyperlinks.add Anchor:=sel.Range, _”

    in order to to type in “(link)” and then select just “link”..

  9. Bartolomeu Says:

    Correction/Update…I added an IF statement to account for whether the cursor is selecting text already or not. If it is not selecting anything, it adds “(link)” with only the word link as the hyperlinked text…if something is selected, then the selection is used for the hyperlink text.

    If Len(sel.Text) = 1 Then
    sel.TypeText Text:=”(link)”
    sel.MoveLeft Unit:=wdCharacter, Count:=1
    sel.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    sel.Text = “link”
    End If

    *hint* if you copy/paste the code here, the quotation marks will throw an error in the compiler so you have to delete and re-type them (or at least I had to).

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: