In Outlook, is there a way to automatically create hyperlinks with varying parameters? Specifically, I'd like to link to work items in Team Foundation Server, but a general solution would be fine too.

For example, if I type "Work Item 12345", I'd like it to automatically create a hyperlink to "mytfs/workitems?id=12345". Ideally, the link text should still be "Work Item 12345."

I tried creating an AutoCorrect entry, but it doesn't match wildcards * or % - it seems it will only work with literals.

I also tried using outgoing message rules, but I was unable to replace content in the email body.

Creating an AutoCorrect replacement for each work item is not a viable solution. Any thoughts?

  • Would you consider using autohotkey? You can make automatic hotstrings very simply. – var firstName Aug 16 '17 at 16:41
  • @varfirstName: I would prefer a purer solution but if it gets the job done it's certainly worth consideration. – johnnyRose Aug 16 '17 at 16:48

Well, I found a solution using AutoHotKey which only runs in Outlook. I'd still prefer an Outlook-only solution, but this works for now.

This code can be used to automatically create links while you'd naturally type something. The obvious caveat is: you'd have to naturally type the URL parameter in order for this to feel seamless. However, this lends itself well to TFS work items.

#SingleInstance force ; Force kill any currently running instances
#IfWinActive,,Message  ; Only do this in Outlook

:?*:Work Item ::
targetWord = Work Item ; The hotstring to match
StringLen,targetWordLength, targetWord ; The length of the hotstring to match
tfsUrl = mytfs/workitems?id= ; Your URL
Input, id, v,{Enter}{Space}{Tab} ; Assign input to the variable id; stop accepting input on Enter, Space, or Tab
If (id = "") ; Undo and return
    SendInput, {Bs}%targetWord% `

StringLen,idLength, id ; Assign the length of id to idLength
idLength++ ; Increment idLength

SendInput {BackSpace %idLength%}%targetWord% %id% ; Backspace and enter the full link text
Send,{Shift down}{Left %targetWordLength%}{Left %idLength%}{Shift up} ; Highlight the full text

Send, ^k ; Insert hyperlink
SendInput, %tfsUrl%%id% ; Send text for hyperlink

Send, {Space} ; If you don't send a space, Outlook might suggest a longer URL which starts with the URL you provided. A space at the end prevents this.
Send, {Enter} ; Enter to exit dialog


When you type "Work Item ", it will remove the text and wait for you to enter a value ending with an Enter, Space, or Tab. When you're done entering your value and you press one of the ending keys, we delete the parameter value, place the desired text back into the input, and add the hyperlink.


Really simple hotstring script for this purpose I just whipped up:

:*:Work Item ::
SendInput, http://mytfs/workitems?id=

Place this in a .ahk file, right click it, and click "Run Script". I've already tested it on my system.

Outlook can do AutoCorrect on this, but I've found it to be spotty at best.

Here's another version of that script that changes it into a custom text link:

:*:Work Item ::
SendInput, http://mytfs/workitems?id=
SendInput, ^+{Left 4}
SendInput, ^k
SendInput, {End}
Sleep, 2000
SendInput, !t
SendInput, +^{Left}
SendInput, Work Item{Space}
SendInput, {Enter}

You'll have two seconds to type in the id before it automatically changes the text and closes.

If you still wish for an Outlook-only solution, go to File > Options > Customize Ribbon. In the right-hand panel, there is a checkbox for Developer. Click it to make Developer features like macros available. I might be able to help you with macros to a certain extent.

  • “Outlook can do AutoCorrect on this, but I’ve found it to be spotty at best.” Can you elaborate on this and how you got it to work, even if it is spotty? – johnnyRose Aug 16 '17 at 17:43
  • Ok so instead of using "Work Item " as the keyword to activate upon, I just used "Work Item". Then I put "mytfs/workitems?id=" in the "Replace with" box. Then, after using the "macro" once, it worked. But when I tried using it a second time in the same email (after erasing the previous link), it didn't do anything. It didn't replace the text, it didn't delete it, it just sat there doing nothing. – var firstName Aug 16 '17 at 17:51
  • It didn't do anything because you deleted the corrected text, temporarily overriding the AutoCorrect. That would work alright, but it'd be really great if the link text remained "Work Item 12345." I've edited my question to reflect that; I apologize for being unclear initially. Still hoping for a pure Outlook solution. – johnnyRose Aug 16 '17 at 18:30
  • Oh ok, I understand. – var firstName Aug 16 '17 at 18:36
  • I edited my answer, @johnnyRose – var firstName Aug 16 '17 at 20:25

I found an Outlook-only answer for you. Outlook supports a programming language called VBA (Visual Basic for Applications).

Use Alt+F11 to access the VBA Editor, then right click under Project1 -> MicrosoftOutlookObjects on "ThisOutlookSession", and click "View Code".

Then paste this code in the editor that appears:

Sub GetValueUsingRegEx()
 ' Set reference to VB Script library
 ' Microsoft VBScript Regular Expressions 5.5

    Dim olMail As Outlook.MailItem
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match

    Set olMail = Application.ActiveInspector.CurrentItem
   ' Debug.Print olMail.Body

    Set Reg1 = New RegExp

    With Reg1
        .Pattern = "Work Item\s*\d*\s*"
        .Global = True
    End With
    If Reg1.Test(olMail.Body) Then

        Set M1 = Reg1.Execute(olMail.Body)

        Debug.Print Split(M1.Item(0), " ")(2)
        olMail.HTMLBody = Replace(olMail.HTMLBody, M1.Item(0), "<a href=" & Chr(34) & "http://mytfs/workitems?id=" & Split(M1.Item(0), " ")(2) & Chr(34) & ">Work Item " & Split(M1.Item(0), " ")(2) & "</a>")
        Debug.Print olMail.HTMLBody
    End If
    End Sub

It will run when you send any message containing "Work Item ####" and automatically change it to a link. You can also make it a hotkey so you can do it while typing the message.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.