Introduction | Example | Tutorial | Applications | Comments

Introduction - Word Hyperlinks

This tutorial will show you how to quickly add hyperlinks to the references in your Word document using a VBA macro. By simply placing your references in square brackets and running this macro, you’ll be able to quickly add and update direct links to your references.

If you want more great VBA tips, please subscribe to my email list.


Example - Word Hyperlinks

Sub HyperlinkReferences()
'---------------------------------------------------------------------------------------------------
'---Script: HyperlinkReferences---------------------------------------------------------------------
'---Created by: Ryan Wells (wellsr.com)-------------------------------------------------------------
'---Date: 05/2016-----------------------------------------------------------------------------------
'---Description: THIS SUBMODULE ADDS HYPERLINKS TO REFERENCES CONTAINED IN SQUARE ------------------
'----------------BRACKETS ( ex: [2.1] or [1] ).  ----------------------------------------------------------
'---------------------------------------------------------------------------------------------------
'---Instructions:-----------------------------------------------------------------------------------
'-----BLOCK 0---------------------------------------------------------------------------------------
'------1) Change the variable ReferenceSection to whatever your reference section is in your--------
'------   document. Set it equal to "" if you refer to your references like [9] instead of [2.9]----
'-----BLOCK 1---------------------------------------------------------------------------------------
'------2) Add the reference hyperlinks to BLOCK 1.--------------------------------------------------
'------3) Change the *max* in dim ref(1 to *max*) to the number of references in your design calc.--
'------4) If you don't have a link to a reference, set that reference equal to "".------------------
'-----BLOCK 2---------------------------------------------------------------------------------------
'---------Changes to BLOCK 2 should not be required.------------------------------------------------
'---------------------------------------------------------------------------------------------------
Application.ScreenUpdating = False
Dim ReferenceSection As String
Dim rng As Range
'***************************************
'*************   BLOCK 0   *************
'***************************************
ReferenceSection = ""

'***************************************
'*************   BLOCK 1   *************
'***************************************
Dim ref(1 To 3) As String
ref(1) = ""
ref(2) = "https://inis.iaea.org/search/searchsinglerecord.aspx?recordsFor=SingleRecord&RN=39099974"
ref(3) = "http://rpd.oxfordjournals.org/content/74/3/163"

'***************************************
'*************   BLOCK 2   *************
'***************************************
For i = 1 To UBound(ref) 'i = 1 to number of references
    Selection.HomeKey wdStory
    
    Selection.Find.ClearFormatting
    With Selection.Find
        If ReferenceSection <> "" Then
            .Text = "[" & ReferenceSection & "." & i & "]"
        Else
            .Text = "[" & i & "]"
        End If
        '.Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
    End With
    
    While Selection.Find.Execute
        If Selection.Hyperlinks.Count > 0 Then
            Selection.Hyperlinks(1).Delete
        End If
        If ref(i) <> "" Then
            Set rng = Selection.Range
            rng.SetRange Start:=rng.Start + 1, End:=rng.End - 1
            Selection.Hyperlinks.Add Anchor:=rng, _
            TextToDisplay:=Mid(Selection.Range.Text, 2, Len(Selection.Range.Text) - 2), _
            Address:=ref(i)
        End If
    Wend
Next i

Application.ScreenUpdating = True
End Sub

Write better macros in half the time
I see people struggling with Excel every day and I want to help. That's why I'm giving away my personal macro library for free. This powerful gift lets you automatically import all my macros directly into your spreadsheet with just one click.

Show me how I can improve my VBA skills for free


Tutorial - Word Hyperlinks

Here’s an excerpt from a report created with Word:

Word Report Excerpt
Word Report Excerpt

The document points to the following references:

Word Report References
Word Report References

Let’s say we have electronic copies of these documents stored on a network drive or available online. Wouldn’t it be great to add hyperlinks to these references so you or the reader of your report can automatically open them? That’s exactly what our macro does.

Directions for how to setup and use the Word VBA macro are included in the comments near the top of our macro example. The important part is that your references must be referred to inside of square brackets.

In our example, we had 3 references so we declared our array variable ref from 1 to 3 using the code Dim ref(1 To 3) As String.

Because we did not have a link to Reference 1, we set that reference, ref(1), equal to the empty string “”.

After setting up our macro and running it, links to all the available references now show up in the report:

Word Report with Hyperlinks
Word Report with Hyperlinks

Notice the variable ReferenceSection is set to the empty string “”. That’s because we refer to our references like [1] instead of something like [2.1]. If your report uses the latter format, simply set your ReferenceSection variable equal to a 2 in BLOCK 0.


Application Ideas

I know this seems like something you can do by hand when you only have a few references, but imagine you have a 50 page report with dozens (or even hundreds) of references. You don’t want to add hyperlinks to those references by hand! This Word macro makes adding or modifying references a breeze.

There’s no doubt that you, your boss, and your audience will thank you for giving them direct hyperlinks to your source references!


Comments

I hope you found this VBA tutorial useful. Submit a comment below if you like this macro or have more questions. As always, subscribe to my email list, share this article on social media and follow me on Twitter and check out our VBA Q&A community for more great VBA content.