Creating hyperlinks from text in an Office document

I needed to add hyperlinks to specific text patterns in a Microsoft Office document. As usual, at least for me, it ended up in a painful trial and error session of VBA programming. So here is the final script for the record. It will transform any text like “JIRA-123” into a hyperlink. Modify it to your own needs.

Sub AddLinks()
With Selection.Find
         .Text = "JIRA\-[0-9]@>"
         .Replacement.Text = ""
         .Forward = True
         .Wrap = wdFindContinue
         .Format = False
         .MatchCase = True
         .MatchWholeWord = True
         .MatchWildcards = True
         .MatchSoundsLike = False
         .MatchAllWordForms = False
         .Execute
    End With
    Do While Selection.Find.Found
      URL = "https://jira.domain.com/browse/" & Selection.Text
      ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=URL, TextToDisplay:=Selection.Text
      Selection.End = Selection.End + 1
      Selection.Collapse wdCollapseEnd
      Selection.Find.Execute
    Loop
End Sub
 
Advertisements
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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