Word.Tips.Net WordTips (Menu Interface)

Occurrences of a Text String within a Document

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.

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
    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

        If Replacements <> 0 Then
            MsgBox _
              "" & .Text & " has been replaced " & _
              CStr(Replacements) & " times with " & _
            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.

Related Tips:

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

viral Parmar    14 Sep 2015, 02:14

i want to search double character alphabets in my document.
where are the find double alphabets i want to expand with 1pt.

this kind of any code is available.
Please give me solution urgent.

Thanks & Regards,
Viral Parmar
Jude Yeaton    17 Mar 2013, 15:53
Another way: search with "Highlight all items found in:" checked, then ^c, paste in another document, and count the lines.
Yves Counasse    16 Oct 2012, 05:36
This tip is interesting but I was wondering if there is a way to create a macro that would flag the occurrences of any frequently repeated word in a text. For example: in a two-page document, the author has used six times the word "facilities", ten times the word "environment", and eight times the word "ozone".
Is it possible to use a macro that would flag (highlight/underline/change font) these words. The aim is to track the overusage of idendical words in nicely written text.
Thank you kindly,

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.