Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Finding Long Sentences.

Finding Long Sentences

by Allen Wyatt
(last updated May 16, 2016)

5

Bruce is looking for some way to have Word automatically mark long sentences in a document. For instance, he may want to have those sentences with more than 20 words marked in some color so that they are easily located.

Fortunately, Word maintains a Sentences collection, accessible through VBA, that consists of each sentence in a document. You can examine each item in this collection (each individual sentence) to determine if it is longer than your desired length. The following macro provides an example of how this is done.

Sub Mark_Long()
    Dim iMyCount As Integer
    Dim iWords As Integer

    If Not ActiveDocument.Saved Then
        ActiveDocument.Save
    End If

    'Reset counter
    iMyCount = 0 

    'Set number of words
    iWords = 20

    For Each MySent In ActiveDocument.Sentences
        If MySent.Words.Count > iWords Then 
            MySent.Font.Color = wdColorRed
            iMyCount = iMyCount + 1
        End If
    Next
    MsgBox iMyCount & " sentences longer than " & _
      iWords & " words."
End Sub

Notice that each sentence is examined, and if it is longer than the desired length (defined by the variable iWords) then the sentence is changed to a red font color. This makes it easy to examine the document and discover which sentences exceed the length you specified.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11909) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Finding Long Sentences.

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

Removing a Directory

Macros allow you to perform all sorts of file-related operations. One such operation allows you to delete a directory. This ...

Discover More

Checking for Either of Two Text Values

Using a formula to find information in a text value is easy. Using a formula to find either of two text values within a ...

Discover More

Comma-Delimited and MS-DOS CSV Variations

Excel provides different file formats you can use to export your worksheet information. One such file format is CSV, or comma ...

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)

Entering a Page Break from the Keyboard

Need to force Word to move text to the top of the next page? It's easy when you use the keyboard shortcut for a page break.

Discover More

Transposing Two Words

A common editing task is to transpose two adjacent words, so that their order is changed. While the task is common, there is ...

Discover More

Moving Section Breaks

Section breaks are used to divide a document into two or more sections that can be independently formatting. If you want to ...

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}] 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 five more than 2?

2016-11-23 07:37:59

Joe

Thanks for this, works great!

If I then go and change some of the sentences to less than 20 words and run the macro again, is there any way to change the sentences that are less than 20 words back to black, rather than staying red?

Thanks!


2014-11-26 15:20:14

Darin Ramsey

Allen--
This is very useful for us, as our group supports a large engineering organization on a Federal project. I've altered your macro to pull the average sentence length from the readability statistics, double it, and change those sentences red. Unfortunately, it appears to be counting punctuation as "words." In a document with an average sentence length of 20.3, a sentence with 37 words (according to the GUI) and five internal marks (a set of parentheses, a semicolon, and two commas) was going red until I removed four of the marks. Which four doesn't make any difference, so it seems to be counting all of them. Do you have any advice on getting the VBA word count to more accurately align with Word's GUI count? The changes in the code are as follows:

After the save, insert this:
Set myRange = ActiveDocument.Content
wordval = myRange.ReadabilityStatistics(6).Value

When setting the iWords value, use this:
iWords = wordval * 2


2013-12-18 02:32:27

Rochelle Fisher

We got an error on the first run, that MySent is not defined. Added: Dim MySent As Object

(At first, defined it as Sentences, but that caused other problems.)


2013-11-26 21:53:50

aiway

hi !
how do i get it to undo the red marks on my file ?
thanks !
a


2013-10-17 14:22:58

Mike Korner

Just wanted to say thanks for this Allen. It was helpful today.


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.

Newest Tips
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.