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: Occurrences of a Text String within a Document.
by Allen Wyatt
(last updated July 21, 2012)
Marc is looking for the fastest, most efficient way—within a macro—to determine a count of how many times a particular text string occurs within a document. Unfortunately there is no way to do this with a simple command or two; instead you need to "step through" a document using the Find and Replace feature of Word.
First, make a temporary copy of your document so that you don't run the risk of messing up your original document. Then use a variable in your macro to count the number of times the desired text gets replaced, and increment the variable every time a replacement occurs. In the following example, the number of times will end up in the variable Replacements. You can then use the value or convert the value to a string to display it.
Sub CountReplacements Dim Replacements As Integer Replacements = 0 Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = InputBox("Enter the text you want to find:") .Replacement.Text = InputBox("Enter the replacement text:") .Forward = True .Wrap = wdFindContinue .Format = False .Execute Replace:=wdReplaceOne Do Until Not .Found .Execute Replace:=wdReplaceOne Replacements = Replacements + 1 Selection.MoveRight Unit:=wdCharacter, Count:=1 Loop If Replacements <> 0 Then MsgBox _ "" & .Text & " has been replaced " & _ CStr(Replacements) & " times with " & _ .Replacement.Text Else MsgBox .Text & " was not found in the document/selection." End If End With End Sub
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (3368) 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: Occurrences of a Text String within a Document.
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!
As you are typing, AutoCorrect provides a "check" that what you are entering doesn't match some pre-defined error triggers. ...Discover More
One of the common things done in macros is to somehow "process" documents, which often means moving the insertion point in ...Discover More
Writing macros often involves selecting different parts of your document so that some sort of processing can be performed. If ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."