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

Copying Red Text to a New Document

Need to copy all the red text in your document to a different document? There are several ways you can approach the task; ...

Discover More

Inserting a Dynamic Word Count in Your Document

Need to know how many words are in your document? You can use the NumWords field to add that statistic, dynamically, to ...

Discover More

Dragging and Dropping Pictures in a Document

Do you like to add pictures to your document just by dragging and dropping? What are you to do if it appears the ...

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)

Finding All Camel-Case Words

If you need to find words that mix uppercase and lowercase characters together, you will appreciate this tip. Here I show ...

Discover More

Count of Underlined or Struck-Through Words

When working with documents (particularly those prepared by others) it is not unusual to need to count words that possess ...

Discover More

Replacing Formatting Functions as a Toggle

Sarra is having a problem getting Find and Replace to behave properly when replacing italic-formatted text. This tip ...

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 8 + 4?

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.