Count # of times a string appears in a doc

M

Minerva

How do I determine how many times a given string appears
in a document?

This will find the string, but can anyone advise me on how
to determine how many times this string is in the range?

with myRange
.Text = "myString"
.Forward = True .Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
end with
 
D

Doug Robbins - Word MVP

Hi Minerva,

Use

' Macro created 27-10-98 by Doug Robbins to get the number of times that a
word appears
'
Dim Message, Title, Default, MyValue
Message = "Enter the word for which you need the number of occurences" '
Set prompt.
Title = "InputBox Demo" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
Selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
.ClearFormatting
Counter = 0
Do While .Execute(FindText:=MyValue, Forward:=True) = True
Counter = Counter + 1
Loop
End With
MsgBox "The word " + MyValue + " appears" + Str$(Counter) + " times."


Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

M

what a speedy response! thank you!!
-----Original Message-----
Hi Minerva,

Use

' Macro created 27-10-98 by Doug Robbins to get the number of times that a
word appears
'
Dim Message, Title, Default, MyValue
Message = "Enter the word for which you need the number of occurences" '
Set prompt.
Title = "InputBox Demo" ' Set title.
Default = "" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
Selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
.ClearFormatting
Counter = 0
Do While .Execute(FindText:=MyValue, Forward:=True) = True
Counter = Counter + 1
Loop
End With
MsgBox "The word " + MyValue + " appears" + Str$(Counter) + " times."


Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top