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.
Note:
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.
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!
When using styles in a document, you can increase your productivity by letting Word know what paragraph style you expect ...
Discover MoreWhen you are working with Word in an organization (regardless of how many people), standardizing styles and their use can ...
Discover MoreUsing styles to format templates can save a lot of time and ensure consistency when working on several documents. Some ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments