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

Default Picture Settings

Place enough pictures, and sooner or later you start wondering if there is a way to set up some default settings for ...

Discover More

Hiding Columns Based on a Cell Value

Need to hide a given column based on the value in a particular cell? The easiest way to accomplish the task is to use a ...

Discover More

Positioning Line Numbers

Line numbers can be added to a document, and is typically done in legal documents. The line numbers appear to the left of ...

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)

Deleting a Large Number of Styles

Styles are a fantastic tool for formatting documents. As you work with documents created by others, you may want to get ...

Discover More

Ensuring that Spell Checking is Enabled in All Styles

Ever want to enable spell checking in all of the styles within a document, but don't want to check each and every one ...

Discover More

Automatically Update Document Styles Setting

Templates and styles are a great way to apply formatting consistently within and across documents. A couple of the ...

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 two more than 7?

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.