Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Highlighting Every Thousandth Character.

Highlighting Every Thousandth Character

Written by Allen Wyatt (last updated August 21, 2020)
This tip applies to Word 97, 2000, 2002, and 2003


Hakan needs a macro that counts the characters (without spaces) in a word document and highlights every 1000th letter. Creating such a macro is rather straightforward—you simply need to examine all the characters in a document, in turn, and only count those that aren't spaces. The following is a simple little macro that will do just that:

Sub CountThousands1()
    Dim J As Long
    Dim X As Integer

    X = 0
    With ActiveDocument
        For J = 1 To .Characters.Count
            If .Characters(J) <> " " Then X = X + 1
            If X = 1000 Then
                .Characters(J).Select
                Selection.Range.HighlightColorIndex = wdYellow
                X = 0
                Beep
            End If
        Next J
    End With
End Sub

The macro is simple enough; it examines the Characters collection, which contains all the individual characters in a document. The problem with the macro is that it is slow—very slow. Word isn't terribly efficient in examining individual characters in this manner. (It appears that each time you reference a member of the Characters collection, Word needs to examined all the characters from the beginning of the document, all over again.)

A different approach is to simply step through the document, expanding a selection until you get to 1,000 non-space characters.

Sub CountThousands2()
    Dim X As Integer
    Dim sRaw As String
    Dim sProc As String

    Selection.MoveRight Unit:=wdCharacter, Count:=1000, Extend:=wdExtend
    While Len(Selection) = 1000
        sRaw = Selection
        sProc = Replace(sRaw, " ", "")
        X = 1000 - Len(sProc)
        While X > 0
            Selection.MoveRight Unit:=wdCharacter, Count:=X, Extend:=wdExtend
            sRaw = Selection
            sProc = Replace(sRaw, " ", "")
            X = 1000 - Len(sProc)
        Wend
        Selection.Collapse Direction:=wdCollapseEnd
        Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Selection.Range.HighlightColorIndex = wdYellow
        Selection.Collapse Direction:=wdCollapseEnd
        Selection.MoveRight Unit:=wdCharacter, Count:=1000, Extend:=wdExtend
    Wend
End Sub

Start this macro with the insertion point at the beginning of the document. The macro then grabs a thousand characters, assigns that selection to a variable (sRaw), creates a variable that has all the spaces removed from it (sProc) and then figures the length of sProc. If it is less than 1,000, then the selection is extended by however many characters it was short and the process is repeated. When the selection contains 1,000 non-space characters, then the highlight is set and the macro goes on to the next block of characters.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (7870) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Highlighting Every Thousandth Character.

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

Speeding Up Large Worksheets

If your worksheet gets large enough, you may notice a severe slowdown when it is recalculated. This tip provides some ...

Discover More

Retaining Formatting After a Paste Multiply

You can use the Paste Special feature in Excel to multiple the values in a range of cells. If you don't want Excel to ...

Discover More

First and Last Names in a Page Header

When you have a worksheet that includes a long list of names, you may want the first and last names on each page to ...

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 (menu)

Setting the Right Indent of a Paragraph in a Macro

Need to format your document using a macro? You can easily set the right margin for an individual paragraph by using the ...

Discover More

Options in Creating New Files

You'd think that Word would be consistent, right? Wrong! Here's one example where Word is anything but consistent when it ...

Discover More

Toggling Font Assignments in a Macro

If you need to quickly switch a text selection from one typeface to another, one way you can do it is with a macro. This ...

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 two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.