4

I have a rather large Word file (+1,100 pages) that contains plain text links of media coverage. For instance, the Word document contains:

The News Leader (AP) Va. police officer who died on her first shift laid to rest http://www.newsleader.com/story/news/nation-now/2016/03/01/va-police-officer-who-died-her-first-shift-laid-rest/81183272/ 03.01.16

Livingston Daily Va. police officer who died on her first shift laid to rest http://www.livingstondaily.com/story/news/nation-now/2016/03/01/va-police-officer-who-died-her-first-shift-laid-rest/81183272/ 03.01.16

(In the Word document, these links are not hyperlinks, just text!)

Currently, the way we acquire the data and merge into Word formats the links as plain text rather than hyperlinks.

I'd like to use VBA to convert all these plain text links into clickable hyperlinks, preserving the link text (e.g., hyperlinks with the anchor text of the link remaining).

I've found examples of code to find and replace specific strings with specific links, for instance:

Sub FindAndHyperlink()
    'define the style
    Dim strStyle As String
    strStyle = "Normal"
    'set the search range
    Dim rngSearch As Range
    Set rngSearch = ActiveDocument.Range
    'set the search string
    Dim strSearch As String
    strSearch = "tuesday"
    '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

But, I can't figure out how to dynamically change the search / select / replace functions.

What I'd like:

  1. Search for "http"
  2. Select entire hyperlink
  3. Make into a hyperlink, not changing the anchor text
  4. Repeat for all instances of plain text hyperlinks

Any suggestions?

1

3 Answers 3

4

Try this:

Sub Hyperlinker()

    Dim Rng As Range

    Set Rng = ActiveDocument.Range
    With Rng.Find
        Do While .Execute(findText:="http:", Forward:=False) = True
            Rng.MoveEndUntil (" ")
            ActiveDocument.Hyperlinks.Add _
                Anchor:=Rng, _
                Address:=Rng.Text, _
                SubAddress:="", _
                ScreenTip:="", _
                TextToDisplay:=Rng.Text
            Rng.Collapse wdCollapseStart
        Loop
    End With

End Sub

After it's done replacing them all, you may have to save and reopen the Word document to have the links start functioning.

7
  • 1
    Thanks, Techie. I'm getting a "Run-time error 4198" "Command Failed" error when I run. Debugger points to: ActiveDocument.Hyperlinks.Add _ Anchor:=Rng, _ Address:=Rng.Text, _ SubAddress:="", _ ScreenTip:="", _ TextToDisplay:=Rng.Text Apr 4, 2016 at 21:12
  • Hmm, interesting.. Which version of Word are you using? Apr 4, 2016 at 21:28
  • Mac 2011. That's probably the issue, right? Apr 4, 2016 at 21:43
  • 2
    Figured it out. The text was in tables throughout the document. Once I stripped those out and ensured that there were spaces after the URL, it worked like a charm. Really appreciate your help. Thank you! Apr 5, 2016 at 18:46
  • 1
    @Ƭᴇcʜιᴇ007 - This was incredibly valuable. I'm a novice among macro novices, and even more so for code.. Once I (barely) came up to speed on deciding where to store it and how to create it, it was easy to copy, save, and run it. Don't know enough to figure out how to do all protocols (http vs https vs www) in a single macro; but did figure out how to create new protocol-specific macros by editing this one.
    – RJo
    Apr 8, 2020 at 20:57
2

A slightly more captious version based on @Techie007's answer and @RJo's enquiry:

Sub Hyperlinker()

    Dim Rng As Range
    Dim startsOfUrlArray As Variant
    Dim startOfUrl As Variant
    
    startsOfUrlArray = Array("https:", "http:", "www")
    
    For Each startOfUrl In startsOfUrlArray
        Set Rng = ActiveDocument.Range
        With Rng.Find
    
                Do While .Execute(findText:=startOfUrl, Forward:=False) = True
                    Rng.MoveEndUntil " "
                    ActiveDocument.Hyperlinks.Add _
                        Anchor:=Rng, _
                        Address:=Rng.text, _
                        SubAddress:="", _
                        ScreenTip:="", _
                        TextToDisplay:=Rng.text
                    Rng.Collapse wdCollapseStart
                Loop
        End With
    Next startOfUrl

End Sub
1
  • That's excellent! Any idea as how one would go about to also make it catch URLs that happen at the end of a paragraph and inside "containers" like parenthesis o quotations? For example like myurl.com. Or in this URL (myurl.com)
    – Luciano
    Apr 9, 2021 at 18:41
0

@Luciano,

Paul Beverley and his team of editors have a macro for converting plain text URLs in reference lists to hyperlinks. It does lob off the "http://" and "https://" part of the URL. But if you need to retain those, just delete those lines in the macro.

Here's the link: https://www.wordmacrotools.com/macros/U/URLlinker.txt.

Cheers!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .