Mac – Find all instances of a text and make it a hyperlink with a macro

macrosmicrosoft wordmicrosoft-officevba

Required

I want to find all occurrences of a single text in a MS Word document, make each occurence a hyperlink and change the generic hyperlink style to one of my chosing.

What I have

As I have no idea on how to achieve the above requirement as a whole, I started on a part of it, namely finding a single instance and adapting it.

So, I recorded a macro, that resulted in the following code. That code I adapted so that the sub could take parameters for the hyperlinkText and the hyperlink subaddress:

Sub AutoDetectHyperlinksForText(hyperlinkText As String, subaddress As String, style As String)
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = hyperlinkText
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
        subaddress:=subaddress, ScreenTip:="", TextToDisplay:= _
        hyperlinkText
    Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    Selection.Style = ActiveDocument.Styles(style)
End Sub

With this it is easy to Call the sub for multiple word instances, such as:

Call AutoDetectHyperlinksForText("broadcasts", "_broadcastService", "Subtle Emphasis")

Question

How can I adapt this macro so that it checks the entire document?

Bonus question

Is there a way to modify the above script so that I can store my selection, and remove the need for the .MoveLeft?

In pseudocode that would be something like this:

Dim mySelect as Selection
mySelect = Selection.Find.Execute
ActiveDocument.Hyperlinks.Add Anchor:=mySelect.Range, Address:="", _
    subaddress:=subaddress, ScreenTip:="", TextToDisplay:= _
    hyperlinkText
mySelect.Style = ActiveDocument.Styles("Subtle Emphasis")

Best Answer

This will find the word "google" (not googles or googled) and hyperlink it to http:\\google.com

It also applies the style

Sub FindAndHyperlink()
    'define the style
    Dim strStyle As String
    strStyle = "Subtle Emphasis"
    'set the search range
    Dim rngSearch As Range
    Set rngSearch = ActiveDocument.Range
    'set the search string
    Dim strSearch As String
    strSearch = "google"
    'set the target address for the hyperlink
    Dim strAddress As String
    strAddress = "http:\\google.com"

    With rngSearch.Find
        Do While .Execute(findText:=strSearch, MatchWholeWord:=True, Forward:=True) = True
            With rngSearch 'we will work with what is found as it will be the selection
                ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress
                .Style = ActiveDocument.Styles(strStyle) 'throw the style on it after the link
            End With
            rngSearch.Collapse Direction:=wdCollapseEnd
            'keep it moving
        Loop
    End With
End Sub

Obviously, if you want to pass arguments you can do that so it can be called with your parameters, but this is the basis of what you'd need.

Related Question