Written by Allen Wyatt (last updated June 22, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
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:
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, 2016, 2019, Word in Microsoft 365, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
When you click a hyperlink in a Word document, the color of the hyperlink changes. If you want to change the color back ...
Discover MoreScreenTips can be a helpful feature in some documents but adding them also means you need to add a hyperlink. Here's a ...
Discover MoreWhen using Word to create content that will end up on the Web, it is helpful to know the probable screen resolution of ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments