Count Macro

R

Robin

I am using Word XP and would like to create a macro that
counts how many times a word (or a series of words) is
used in the document and return that value.

Help?!!!!
 
L

Lerxst

Function CountInDoc(ByVal d As Document, findstring As
String) As Long
Set rg = d.Range
With rg.Find
.Text = findstring
.Wrap = wdFindStop
'[Other find settings, such as whole words only,
wrap, match case, etc.]
.Execute
End With
While rg.Find.Found = True
myCount = myCount + 1
rg.Start = rg.End
rg.End = ActiveDocument.Range.End
rg.Find.Execute
Wend
CountInDoc = myCount
End Function
 
H

Helmut Weber

Hi Robin,
create a list of all words in the document,
remove all double entries from the list,
step through the list, search for each item,
and count the successful finds.
As a start have a look at:
http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm
If you replace a word by itself you get the number of occurences.
But what is a word in MS-Word? Certainly not what you would expect.
I'd suppose to make your way through this, if you really want it,
by splitting the whole job into several tasks and post for them
in microsoft.public.word.vba. Like
"list all words in a document"
"remove all double entries from a list"
....
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
R

Robin

Could this be set up so that the user can enter what word
(or words) they want to count? For example, if I wanted
to count how many times "United States of America" was
used in a document, could something pop up on the screen
for me to enter the text I want to search for, then count
and return the number of occurrences?

Thanks again!
-----Original Message-----
Function CountInDoc(ByVal d As Document, findstring As
String) As Long
Set rg = d.Range
With rg.Find
.Text = findstring
.Wrap = wdFindStop
'[Other find settings, such as whole words only,
wrap, match case, etc.]
.Execute
End With
While rg.Find.Found = True
myCount = myCount + 1
rg.Start = rg.End
rg.End = ActiveDocument.Range.End
rg.Find.Execute
Wend
CountInDoc = myCount
End Function
-----Original Message-----
I am using Word XP and would like to create a macro that
counts how many times a word (or a series of words) is
used in the document and return that value.

Help?!!!!
.
.
 
L

Lerxst

Yes. Keep the function as is. Add the follwoing Sub to
the module:

Sub CountStringInDoc()
Dim findthis, messg As String, thenum As Long
findthis = InputBox("Enter the word or phrase to
count.", "Count in Document")
thenum = CountInDoc(ActiveDocument, findthis)
If thenum = 1 Then messg = " time in this document."
Else messg = " times in this document."
messg = "'" & findthis & "' occurs " & thenum & messg
MsgBox messg, , "Count in Document"
End Sub

As you use this keep in mind that
the .MatchWholeWord, .MatchCase, etc. properties as set
in the original function will affect the results. If you
want to be able to specify those properties at run-time,
you'll need to design your own userform. That would be a
good learning project, by the way.

-----Original Message-----
Could this be set up so that the user can enter what word
(or words) they want to count? For example, if I wanted
to count how many times "United States of America" was
used in a document, could something pop up on the screen
for me to enter the text I want to search for, then count
and return the number of occurrences?

Thanks again!
-----Original Message-----
Function CountInDoc(ByVal d As Document, findstring As
String) As Long
Set rg = d.Range
With rg.Find
.Text = findstring
.Wrap = wdFindStop
'[Other find settings, such as whole words only,
wrap, match case, etc.]
.Execute
End With
While rg.Find.Found = True
myCount = myCount + 1
rg.Start = rg.End
rg.End = ActiveDocument.Range.End
rg.Find.Execute
Wend
CountInDoc = myCount
End Function
-----Original Message-----
I am using Word XP and would like to create a macro that
counts how many times a word (or a series of words) is
used in the document and return that value.

Help?!!!!
.
.
.
 
R

Robin

Thanks! I will practice, practice, practice.
-----Original Message-----
Yes. Keep the function as is. Add the follwoing Sub to
the module:

Sub CountStringInDoc()
Dim findthis, messg As String, thenum As Long
findthis = InputBox("Enter the word or phrase to
count.", "Count in Document")
thenum = CountInDoc(ActiveDocument, findthis)
If thenum = 1 Then messg = " time in this document."
Else messg = " times in this document."
messg = "'" & findthis & "' occurs " & thenum & messg
MsgBox messg, , "Count in Document"
End Sub

As you use this keep in mind that
the .MatchWholeWord, .MatchCase, etc. properties as set
in the original function will affect the results. If you
want to be able to specify those properties at run-time,
you'll need to design your own userform. That would be a
good learning project, by the way.

-----Original Message-----
Could this be set up so that the user can enter what word
(or words) they want to count? For example, if I wanted
to count how many times "United States of America" was
used in a document, could something pop up on the screen
for me to enter the text I want to search for, then count
and return the number of occurrences?

Thanks again!
-----Original Message-----
Function CountInDoc(ByVal d As Document, findstring As
String) As Long
Set rg = d.Range
With rg.Find
.Text = findstring
.Wrap = wdFindStop
'[Other find settings, such as whole words only,
wrap, match case, etc.]
.Execute
End With
While rg.Find.Found = True
myCount = myCount + 1
rg.Start = rg.End
rg.End = ActiveDocument.Range.End
rg.Find.Execute
Wend
CountInDoc = myCount
End Function
-----Original Message-----
I am using Word XP and would like to create a macro that
counts how many times a word (or a series of words) is
used in the document and return that value.

Help?!!!!
.

.
.
.
 

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