Finding Punctuation-Free Paragraphs

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


3

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.

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

Using Function Keys

Want a great list that shows the purpose of each function key in Word? Here's the detail you need!

Discover More

Inserting Video into Worksheets

You can add all sorts of objects to your workbooks, including video clips. Here's the pros and cons (along with the ...

Discover More

Drawing Lines

Lines are one of the most common graphic elements to be added to documents. Here's how you can add the lines you want.

Discover More

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!

More WordTips (ribbon)

Selectively Find and Replace Page Borders

Using Find and Replace you can both find and replace graphics in your document. Replacing graphics selectively is a bit ...

Discover More

Replacing a Colon in a Sequence

Sometimes you'll run across the need to replace a very specific sequence of characters in your document. It is for these ...

Discover More

Replacing the Space between the Last Two Words of Each Paragraph

Some replacement tasks can seem daunting, but Word provides some great tools for making just about any replacement you ...

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


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.