Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2004

Tip: Looking for answers? Try searching our database.

Count Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robin - 30 Jun 2004 15:00 GMT
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?!!!!
Lerxst - 30 Jun 2004 16:22 GMT
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
[quoted text clipped - 3 lines]
>Help?!!!!
>.
Robin - 30 Jun 2004 17:02 GMT
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
[quoted text clipped - 24 lines]
>>
>.
Lerxst - 30 Jun 2004 19:06 GMT
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
[quoted text clipped - 36 lines]
>>
>.
Robin - 30 Jun 2004 20:44 GMT
Thanks!  I will practice, practice, practice.

>-----Original Message-----
>Yes. Keep the function as is. Add the follwoing Sub to
[quoted text clipped - 63 lines]
>>
>.
Helmut Weber - 30 Jun 2004 16:33 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.