Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. 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: Highlighting Every Thousandth Character.
Written by Allen Wyatt (last updated September 21, 2019)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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 examine 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:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (7871) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Highlighting Every Thousandth Character.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!
Got some numbers you need spelled out? Here's a handy macro that can convert numbers like "123" to words like "one ...
Discover MoreOne of the easiest ways to quickly access a macro is to assign it to the Quick Access toolbar. Here's how you can make ...
Discover MoreOne of the most common ways of creating macros is to use Word's macro recorder. This tip shows how easy it is to use the ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments