Replacing the Style of a Paragraph that Follows a Heading

Written by Allen Wyatt (last updated April 16, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


1

Charles knows that Word allows you to find and replace using styles (search for a style and replace it with another style). He wants to find and replace a style that occurs after another one. For example, he wants to search for any paragraph that comes after a heading (1, 2, 3, or 4) and replace that paragraph's style with a different style. Doing this manually in a 400-page document is enough to drive him off the cliff with boredom!

There is no way to do this using the built-in Find and Replace capabilities of Word. However, this can be done using a macro. All the macro needs to do is to step through every paragraph in the document and, if the paragraph is styled with a heading style, change the heading style of the paragraph that follows it. The macro is easy to implement because it doesn't delete or add any content, it only adjusts paragraph styles.

Sub ConvertAfterHeads()
    Dim J As Long
    Dim dStart As Date
    Dim dFinish As Date
    Dim lPar As Long
    Dim lChg As Long
    Dim sTemp As String

    dStart = Time()
    lChg = 0
    lPar = ActiveDocument.Paragraphs.Count

    For J = 1 To lPar - 1
        If ActiveDocument.Paragraphs(J).Style Like "Heading [1-4]" Then
            ActiveDocument.Paragraphs(J + 1).Style = "MyNewStyle"
            lChg = lChg + 1
        End If

        If J Mod 500 = 0 Then DoEvents
        StatusBar = J & " of " & lPar & " (" & Format(J / lPar, "##0.00%") & ")"
    Next J
    dFinish = Time()

    sTemp = "Start: " & Format(dStart, "hh:mm:ss") & vbCr
    sTemp = sTemp & "Finish: " & Format(dFinish, "hh:mm:ss") & vbCr
    sTemp = sTemp & "Elapsed: " & Format(dFinish - dStart, "hh:mm:ss") & vbCr
    sTemp = sTemp & "Total Paragraphs: " & lPar & vbCr
    sTemp = sTemp & "Changed Paragraphs: " & lChg
    MsgBox sTemp
End Sub

The heart of this macro is the For...Next loop that steps through each paragraph (except the last one) and makes a style change if the paragraph style is any heading, 1 through 4. You'll need to make a change to the macro to reflect your desired new style; just change "MyNewStyle" to the name of your desired style, within quotes. When the macro is complete, you'll see a summary of how long the macro took and how many changes were made.

In testing this macro, I ran it on a 450-page document containing just over 9,500 paragraphs. The macro took just under 15 minutes to complete. The speed of the macro on your system depends on several factors, including the speed of your computer and the characteristics of your document.

One thing I noticed in running the macro on such a long document is that it starts out very quickly, but as it processes paragraphs later in the document (say, after about paragraph number 3,000 or so), the macro starts to slow down noticeably. This has to do with the way that the individual paragraphs are accessed in the Paragraphs collection (using the J variable) and is part and parcel to working through each paragraph in this manner. That is why I included the line that calls DoEvents—in case you want to "bail out" of the macro, before complete, by pressing Ctrl+C.

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 (12868) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

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

Creating Point Pages

Want to add a page, with a different page number, in Word without affecting the entire document? The solution is a bit ...

Discover More

Examining Styles and Macros in a Template

Templates are very powerful with the ability to contain both styles and macros. If you want to see what styles and macros ...

Discover More

Turning Off Automatic Capitalization in Lists

By default, Word capitalizes letters that it thinks designate the beginning of a sentence. This includes at the beginning ...

Discover More

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!

More WordTips (ribbon)

Getting Rid of Modify Style Message

When you apply styles to a paragraph, you may periodically see a message asking if you want to reapply the style or ...

Discover More

Turning Off Smart Quotes for Specific Styles

Smart quotes can be helpful in making a great-looking document, but at times, they can be a real pain. Wouldn’t it be ...

Discover More

Keeping the Styles Pane Open by Default

Word doesn't provide a way that you can display the Styles task pane by default. If you get tired of manually displaying ...

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 1 + 1?

2022-04-16 09:45:41

Ken Endacott

The macro ConvertAfterHeads is very slow because it uses paragraph item numbers rather that stepping through the Paragraphs collection with a For Each statement.

The difference in speed is dramatic, what took 15 minutes can be accomplished in less than 2 seconds.

The macro ConvertAfterHeadsFAST below has this approach. It also corrects a bug in the original macro that did not take into account where a heading paragraph follows a previous heading. Because it is very fast there is no need to monitor progress.

Sub ConvertAfterHeadsFAST()
Dim aPara As Paragraph
Dim skipSW As Boolean
Dim lchg As Long
Dim TA As Single
TA = Timer
skipSW = False
lchg = 0
For Each aPara In ActiveDocument.Paragraphs
If skipSW = True Then
If Not aPara.Style Like "Heading [1-4]" Then
aPara.Range.Style = "MyNewStyle"
lchg = lchg + 1
skipSW = False
End If
Else
If aPara.Style Like "Heading [1-4]" Then skipSW = True
End If
Next aPara
MsgBox lchg & " Changed Paragraphs. " & Timer - TA & " seconds"
End Sub


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.