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

Cannot Convert a Document File to a Readable Document

Open a Word document file, and you can start to make edits and changes to your heart's content. But what happens if the ...

Discover More

Protecting Styles

If you spend a lot of time getting your document styles set "just right," you don't want to take the chance that they ...

Discover More

Deriving an Absolute Value in a Macro

Need to figure out an absolute value within your macro code? It's easy to do using the Abs function, described in this tip.

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)

Shortcut Key to Delete a Paragraph

There are numerous ways you can delete paragraphs as you are editing your document. This tip looks at a couple of the ...

Discover More

Speeding Up Cursor Movement

If you use the arrow keys to move the insertion point through the document, you may have noticed that it can be slow ...

Discover More

Using Optional Hyphens

Adding hyphens to your document can affect the way in which Word wraps text from one line to the next. Optional hyphens, ...

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 nine minus 4?

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.