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

Distributing Columns Evenly

When you want the horizontal space in a table to be divided evenly among the columns in the table, you'll love this tip. ...

Discover More

Copying a Cell without Formatting

When you are copying a cell from one place to another (perhaps even to a different worksheet), you may not want to copy ...

Discover More

Finding Missing Spaces before Numbers

If you want to insert a space between letters and digits in your document, you have a couple of tasks to perform. First, ...

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)

Intelligently Starting a New Paragraph

When using styles in a document, you can increase your productivity by letting Word know what paragraph style you expect ...

Discover More

Deploying Standard Styles through an Organization

When you are working with Word in an organization (regardless of how many people), standardizing styles and their use can ...

Discover More

Using Hidden Styles

Using styles to format templates can save a lot of time and ensure consistency when working on several documents. Some ...

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

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.