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


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.


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


Printing a Bookmark List

Need to know what bookmarks are defined in a document? Here's a macro that creates a list of all your bookmarks so that ...

Discover More

Setting Grammar-Checking Options

When Word checks the grammar it thinks you are using in your prose, it follows a set of rules. Fortunately, the program ...

Discover More

Understanding the VLOOKUP Function

Functions are at the heart of Excel's power in working with data. One of the most misunderstood functions provided by ...

Discover More

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!

More WordTips (ribbon)

Turning Off a Dictionary for a Style

There may be some paragraphs in a document that you don't want Word to spell- or grammar-check. You can 'turn off' the ...

Discover More

Preventing Changes to Styles in Documents

Have you ever created a template only to have the styles within it changed as they were used within a document? Here are ...

Discover More

Moving All Headings Down One Level

Do you want to demote all the headings in your document by one level? There are some great tools built into the program ...

Discover More

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


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 - 0?

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


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.