Finding Punctuation-Free Paragraphs

by Allen Wyatt
(last updated February 12, 2022)

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

Canceling a Menu

Want to cancel whatever menu command you started to use in Excel? The normal method is by pressing Esc, but this is not ...

Discover More

Hyperlinks in Shared Workbooks

Inserting a hyperlink into a workbook that is shared with others is not possible in Excel. Here's what you can do about it.

Discover More

Putting Style Names Next to Paragraphs on a Printout

If you use styles to format your document, you might want a way to print the document and show, to the left of each ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

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

Keyboard Control of the Find and Replace Dialog Box

Hate to take your hands off the keyboard? This tip explains how you can use the keyboard to work with the Find and ...

Discover More

Finding Missing Spaces before Numbers

If you want to insert a space between letters and digits in your document, you have a couple of tasks to perform. First, ...

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

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.