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

Underlining Section References Automatically

by Allen Wyatt
(last updated May 24, 2018)

5

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.

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, and 2016. You can find a version of this tip for the older menu interface of Word 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

Curving Text Around the Edge of a CD

Word works great with text, but not so great if you need to do some specialized things with the text, such as printing it ...

Discover More

Inserting Foreign Characters

It is not unusual to need to insert foreign characters (often called diacritical marks) as part of your typing. Word ...

Discover More

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Adjusting the Width of Characters

Need to adjust how your characters look horizontally? Word provides an easy way you can scale the horizontal appearance ...

Discover More

Discovering the RGB Value of a Custom Text Color

Word allows you to easily change the color of the text in your document. If you get a document from someone else, you may ...

Discover More

Copying Character Formatting

If you are applying character formatting directly to text rather than using a character style you can copy it from one ...

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}] 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 eight minus 5?

2015-05-12 12:24:47

Maggie

Discovered my mistake. The syntax should be {1,4} to find any number from 1 to 4.


2015-05-12 12:13:24

Maggie

Peter--

I did do some more reading on wildcards and came up with a search for the following:

.Text = "Section [0-9a-zA-Z]{1,2,3,4,}"

which should at least cover any number of digits/characters up to 4 places. Such as: Section 12, Section ABC, Section a, but I get an error message when running the macro "The Find What text contains a Pattern Match expression that is not valid." I must be doing something wrong, but can't determine what it is. Any chance you can point me in the right direction? I can't seem to find many specific examples of such a search--the descriptions are more explanatory, as in {n} "finds exactly the number of n occurrences". Thanks!


2015-05-12 11:41:25

Maggie

Peter--That's very helpful--thank you!


2015-05-11 20:04:07

Peter

Yes, but as Allen said, the real challenge is to standardise your searches. Someone could write a macro for the specific examples you’ve given, then someone else would say, “But what about … ?”

My advice is to work on your last comment, and aim to gradually build up your understanding of wildcard searches. For instance, try a Find search (rather than Find and Replace; that way you don’t risk spoiling your data). Then experiment with items in your search and see what happens. A good resource is word dot mvps dot org. Go to their site and search for “using wildcards”.

Here’s an example. The reason the macro didn’t pick up line 6 is because it’s coded to find single digit numbers. [0-9] means “Find any number in the range 0 to 9”, and {1,} means “Find one instance”. So [0-9] {1,} would have found 7 followed by the dot, but it wouldn’t find 7101 followed by the dot. [0-9] {4,} would produce the opposite result. [0-9] {1,4,} would find both instances. You need to practice.


2015-05-08 14:31:57

Maggie

I tried this macro on the following text, and it worked on 2, 4, and 5 only, since they're the ones that contain a period after an initial digit.

1. This is a Section 1 reference.
2. This is a Section 1.1 reference
3. This is a Section 1(b) reference
4. This is a Section 1.1(b)(c)(1) reference
5. This is a Section 1.1(b)(a) reference
6. This is a Section 7101-5(a) reference.

Is there a way to make it recognize the other ones as well? Wildcards confuse me.


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.

Newest Tips
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.