Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Underlining Section References Automatically.

Underlining Section References Automatically

Written by Allen Wyatt (last updated June 26, 2018)
This tip applies to Word 97, 2000, 2002, and 2003


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:

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 (497) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Underlining Section References Automatically.

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

Changing Label Printing Order

If you want to change the order in which labels are printed when doing a mail merge, Word doesn't provide many options. ...

Discover More

AutoFilling with Weekdays

Need to fill a range of cells with the days of the week? Excel makes it easy to do so using AutoFill.

Discover More

Applying Consistent Shading to a Table

Formatting tables can be very time consuming. When you get a document from another person, you can spend a lot of time ...

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 (menu)

Complex Compound Formatting

Sometimes it can seem that the formatting needs of your document can easily outstrip the capabilities of Word. This is ...

Discover More

Making Text Bold

Want a cool shortcut to make your text bold? Here's a method that fits in wonderfully with how things are done in the ...

Discover More

Applying Bold Italics

Applying bold and italics formatting to text is easy in Word. If you want to apply bold and italics simultaneously, you ...

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 5 - 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.