Making Hyperlinks from Coded Text

Written by Allen Wyatt (last updated October 15, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016


2

Jim's Word documents have a lot of URLs that are not active hyperlinks. Each URL is "bracketed" by the text [lt] and [gt]. So, for instance, a URL might appear as [lt]https://www.tips.net[gt]. Jim wonders if there is a way that a macro could find the text between the bracket codes, create a hyperlink from the found text, and then delete the bracket codes.

Searching for such instances is a snap if you use the wildcard capabilities of Find and Replace. All you need to do is search for "[lt]*[gt]" (without the quote marks) and make sure that you specify you are doing a wildcard search. The tricky part comes in getting rid of the bracket codes and making whatever was between them into an active hyperlink.

To do all of this in one pass, you really need to use a macro, as Jim suspected. Even so, the macro doesn't need to be terribly complex. Here's an example of one that will do the trick:

Sub AddHyperLinksAndClearBrackets()
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "\[lt\]*\[gt\]"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Selection.Find.Execute
    Do While Selection.Find.Found
        Selection = Mid(Selection, 5, Len(Selection) - 8)
        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
          Address:=Selection, SubAddress:=""
        Selection.Find.Execute
    Loop
End Sub

Note that whenever a match is found (in the Do While loop), there are two commands that do the bulk of the work. The first is this one:

        Selection = Mid(Selection, 5, Len(Selection) - 8)

This command line sets the selection equal to the selection minus the beginning and ending bracket codes. In effect, it deletes the bracket codes, leaving whatever was in the middle. This is a slick little trick that precludes the need to perform a separate find-and-replace operation to get rid of the bracket codes.

After the removal of the bracket codes, the next line is executed:

        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
          Address:=Selection, SubAddress:=""

This line converts the remaining selection (which should be a text-only hyperlink) into an active hyperlink.

The macro is short, sweet, and runs extremely quickly, regardless of how many coded hyperlinks you have in your document.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (672) applies to Microsoft Word 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Making Macros Run Faster

Designing a macro to make it run faster.

Discover More

Adding Page Borders to a Printout

Need a full-page border on your Excel printouts? It's not as easy to get one as you might wish. There are a few ways you ...

Discover More

Using the Object Browser

Efficiently navigating through a document, particularly as it gets longer, can be a perpetual challenge. One tool you can ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

ScreenTips without Hyperlinks

ScreenTips can be a helpful feature in some documents but adding them also means you need to add a hyperlink. Here's a ...

Discover More

Creating an E-mail Message from the Current Document

In some earlier versions of Word, you could type an e-mail in your document and then have Word e-mail it, as a message, ...

Discover More

Adding a ScreenTip

Need to add a ScreenTip to your document? It's easy to do, provided you are adding a hyperlink.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2017-05-22 11:07:29

Andrew

Instead of the ActiveDocument.Hyperlinks.Add command, couldn't you also use the following to have Word add it for you (with the advantage this method could be more generally used by accessing the whole panopoly of autoformat/autocorrect options?

Options.AutoFormatAsYouTypeReplaceHyperlinks = True ' Optional since this is normally already set to true
Selection.Range.AutoFormat

Andy.


2017-05-20 11:27:51

Jim Lyons

Sweet! Thank you so much! -- Jim


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.