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

Creating Styles

Standardize the formatting in your Excel workbooks quickly and easily with the Style feature. Here's how to use it.

Discover More

Understanding Single Line Spacing

Single line spacing, the default spacing used in a paragraph, allows Word to adjust the spacing of individual lines in a ...

Discover More

Counting Lists

Word makes it easy to add both numbered lists and bulleted lists to your document. If you are working with longer ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Finding and Deleting Rows

Got a table that contains rows you want to delete? Deleting one or two rows in a table is easy; deleting a bunch of rows ...

Discover More

Replacing Multiple Spaces with Tabs

If you get a document or some text that has multiple consecutive spaces used to align information, you'll undoubtedly be ...

Discover More

Replacing without Automatically Finding

When you use Word's Find and Replace capability, clicking the Replace button performs the replacement and automatically ...

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 four minus 0?

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.