Agnes asked if there is a way to automatically underline all instances of the word "Section" and any ancillary information following the word. For example, a document might contain text such as "Section 2.3(b)(i)" or "Section 5.21" or "Section 12.12(a)" and Agnes wants to find this text and have it all be underlined.
There are a couple of things to try. First of all, you could do a standard Find and Replace, but only if you can somehow make the text a little more "standard." Without some sort of a pattern that can be matched, it is virtually impossible to do a Find and Replace that will find all possible instances of the text.
A possibility, however, is to record a macro that does look at all the possibilities. It wouldn't necessarily be a simple macro, as it would need to find the word "Section" followed immediately by a space, a digit, a period, more digits, and then (optionally) everything up to and including a closing parenthesis. That is quite a bit of text analysis that needs to occur.
The place to start is with a wildcard search. The following search phrase will find the word Section followed by a "number dot number" pattern:
Section [0-9]{1,}.[0-9]{1,}
If this search pattern is used in a macro, then the macro can, after each successful find of the text, start expanding what was found and see if it contains parenthetical characters. The following macro will perform this task.
Sub ULWords() Selection.Find.ClearFormatting With Selection.Find .Text = "Section [0-9]{1,}.[0-9]{1,}" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute While Selection.Find.Found Selection.MoveRight Unit:=wdCharacter, _ Count:=1, Extend:=wdExtend While Right(Selection.Text, 1) = "(" Selection.MoveRight Unit:=wdCharacter, _ Count:=1, Extend:=wdExtend While Right(Selection.Text, 1) <> ")" Selection.MoveRight Unit:=wdCharacter, _ Count:=1, Extend:=wdExtend Wend Selection.MoveRight Unit:=wdCharacter, _ Count:=1, Extend:=wdExtend Wend Selection.MoveRight Unit:=wdCharacter, _ Count:=-1, Extend:=wdExtend Selection.Font.Underline = True Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.Find.Execute Wend End Sub
Note that the macro uses a wildcards search at the beginning to find all instances of the word "Section" followed by the "number dot number" pattern. If an instance is found, then it is extended by one character. If that character is an opening parenthesis then the selection is extended until a closing parenthesis is found. This process of finding opening/closing parentheses is continued, and when no more sets are located the entire extended selection is underlined. This process continues until the entire document has been searched.
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 (11516) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Office 365. You can find a version of this tip for the older menu interface of Word here: Underlining Section References Automatically.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Highlighting text, using the Highlight tool, is a great way to mark up a document. Normally you need to use the toolbar ...
Discover MoreDo you want a superscript and subscript character to appear directly above each other? There are multiple ways you can ...
Discover MoreThin spaces are a typographic device that allows you add a bit of space between elements of a document. There are no thin ...
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.
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments