Written by Allen Wyatt (last updated February 12, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
Johann has a rather long document, and there are some paragraphs that have no punctuation in them. Johann would like to find these paragraphs, skipping any that use the built-in heading styles (Heading 1, Heading 2, etc.). He wonders about the best way to locate these punctuation-free paragraphs.
The only way to accomplish a task such as this would be to use a macro. Fortunately, such a macro can run very, very quickly, even on long documents. Consider the following macro:
Sub MarkNoPunc() Dim sPunc As String Dim sParRaw As String Dim sParStyle As String Dim bFoundPunc As Boolean Dim J As Integer Dim K As Integer sPunc = "!?.,;:" For J = 1 To ActiveDocument.Paragraphs.Count sParStyle = ActiveDocument.Paragraphs(J).Style ActiveDocument.Paragraphs(J).Range.HighlightColorIndex = wdAuto If Left(sParStyle, 8) <> "Heading " Then bFoundPunc = False sParRaw = ActiveDocument.Paragraphs(J).Range.Text For K = 1 To Len(sPunc) If InStr(sParRaw, Mid(sPunc, K, 1)) > 0 Then bFoundPunc = True Next K If Not bFoundPunc Then ActiveDocument.Paragraphs(J).Range.HighlightColorIndex = wdYellow End If End If Next J End Sub
The macro steps through each paragraph in the document, assigning the name of the paragraph's style to the sParStyle variable, and then making sure that the paragraph isn't highlighted. (This is done by setting the .HighlightColorIndex property equal to the wdAuto constant.) The macro then checks if the style begins with the text "Heading ", and if it doesn't, then it stuffs the text of the paragraph into the sParRaw variable.
The text is checked against each character in the sPunc string, and if the character is found, then the bFoundPunc variable is set to True. After checking each character, then the paragraph is highlighted in yellow if bFoundPunc is still False.
You'll want to pay special attention to the contents of the sPunc variable. As shown in the macro, it contains six different punctuation characters. Change the contents of sPunc to make sure it contains any character you want used to disqualify the highlighting of a paragraph.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12838) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!
In plain-text documents, it is not uncommon to see asterisks used around text to indicate what should be considered bold ...
Discover MoreUsing Find and Replace you can both find and replace graphics in your document. Replacing graphics selectively is a bit ...
Discover MoreDo you want to quickly search for any footnote or endnote marks in your document? Word makes it easy using the standard ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-02-20 06:47:31
Ken Endacott
My second attempt at a macro brings the run time down to 1.3 seconds. It is a lot better than the 14 minutes of MarkNoPunc or even the 71 seconds of my first macro. It shows what a bit of optimisation can do.
Sub NoTrailingPunct2()
Dim sPunc As String
Dim aPara As Paragraph
Dim txt As String
sPunc = "!?.,;:"
For Each aPara In ActiveDocument.Paragraphs
If Left(aPara.Style.NameLocal, 7) <> "Heading" Then
txt = aPara.Range.Text
txt = RTrim(Left(txt, Len(txt) - 1))
txt = Right(txt, 1)
If InStr(sPunc, txt) = 0 Then _
aPara.Range.HighlightColorIndex = wdBrightGreen
End If
Next aPara
End Sub
2022-02-15 01:51:16
Arya
Wow! Thank you so much! Great Macro
2022-02-12 21:45:21
Ken Endacott
The macro MarkNoPunct has problems.. The macro is very slow because it uses the code:
For j = 1 To ActiveDocument.Paragraphs.Count
rather than:
For Each aPara In ActiveDocument.Paragraphs
Applied to a 200 page document (not a particularly large document) the former took 14 minutes but the macro modified to use the latter took 90 seconds.
Furthermore, it will not highlight a paragraph that does not end with a punctuation character if there is a punctuation character elsewhere in the paragraph.
The following macro NoTrailingPunct will highlight paragraphs that do not end with a punctuation character. It does not check for punctuation between sentences. Applied to the 200 page document it took 71 seconds.
Sub NoTrailingPunct()
Dim sPunc As String
Dim bFoundPunc As Boolean
Dim k As Integer
Dim aPara As Paragraph
Dim txt As String
sPunc = "!?.,;:"
For Each aPara In ActiveDocument.Paragraphs
If Left(aPara.Style.NameLocal, 7) <> "Heading" Then
bFoundPunc = False
txt = aPara.Range.Text
' --- Remove return and trailing spaces and tabs ---
txt = RTrim(Left(txt, Len(txt) - 1))
txt = Right(txt, 1)
For k = 1 To Len(sPunc)
If txt = Mid(sPunc, k, 1) Then bFoundPunc = True
Next k
If Not bFoundPunc Then aPara.Range.HighlightColorIndex = wdBrightGreen
End If
Next aPara
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