PDA

View Full Version : Solved: How to count occurences of a string in a doc?



RonMcK
07-30-2008, 11:07 AM
Hi, All,

I need some help getting the following shippet of code to count the number of instances of a string in a document. My test document has 4 instances of the string scattered through it, but my code finds none of them because .FOUND is returning False.

Sub CountAWord()
Dim intRowCount As Long ' was integer
Dim aRange As Range
Dim response As Long
intRowCount = 0
Set aRange = ActiveDocument.Range
With aRange.Find
Do
.Text = "ID3" ' the word I am looking for
Debug.Print aRange.Find.Text
Debug.Print ActiveDocument.Name
Debug.Print .Found
.Execute
If .Found Then
intRowCount = intRowCount + 1
End If
Loop While .Found
End With
response = MsgBox("I counted " & Str(intRowCount) & vbCrLf & " in doc: " & ActiveDocument.Name, vbOKOnly, "Number of Instances")
Set aRange = Nothing
End Sub

Is there an easier way, than the above code, to count the number of occurrences of a keyword (or phrase or string) in a document?

Thanks!

RonMcK
07-30-2008, 05:36 PM
Here is a solution to my counting question that works better, based on work by Malcolm (mdmackillop) in the Excel forum and an assist he got from here (http://www.tribbs.co.uk/showtip.php?myID=61&start=23).

Sub CountSpecificStringInWordDoc2()
Dim txt As String, Lgth As Long, Strt As Long
Dim i As Long
Dim oRng As Range
Dim Tgt As String
Dim arr()
ReDim arr(10)
Dim response As Long
'Set parameters
txt = InputBox("String to find")
Lgth = InputBox("Length of string to return")
Strt = Len(txt)
'Return data to array
With Selection
.HomeKey unit:=wdStory
With .Find
.ClearFormatting
.Forward = True
.Text = txt
.Execute
While .Found
arr(1) = arr(1) + 1
Set oRng = ActiveDocument.Range _
(Start:=Selection.Range.Start + Strt, _
End:=Selection.Range.End + Lgth)
oRng.Start = oRng.End
.Execute
Wend
End With
End With
response = MsgBox("I found: " & Str(arr(1)) & " instances of the string: " & txt & _
vbCrLf & " in the document: " & ActiveDocument.Name, vbOKOnly, "Number of occurences")
End Sub
I'm still curious to know if there is a more elegant way to get the count of the number of occurences of a string in a Word doc.

Thanks!

mdmackillop
08-04-2008, 12:40 PM
I'm still curious to know if there is a more elegant way to get the count of the number of occurences of a string in a Word doc.
How about using Find/Replace? Find "Text" Replace "^&"
See here (http://wordtips.vitalnews.com/Pages/T001833_Generating_a_Count_of_Word_Occurrences.html) and here (http://support.microsoft.com/default.aspx?kbid=240157)

mdmackillop
08-22-2008, 12:02 PM
I came across this methodology. It will count strings, but also as part of words.


Option Compare Text
Sub CountOccurences()
Dim MyDoc As String, txt As String, t As String
MyDoc = ActiveDocument.Range.Text
txt = InputBox("Text to find")
t = Replace(MyDoc, txt, "")
MsgBox (Len(MyDoc) - Len(t)) / Len(txt) & " occurrences of " & txt
End Sub

MWE
08-24-2008, 02:27 PM
here is some brute force code that is simplier and seems to work. It does not differentiate between "words" and text string.

Sub CountWords()
Dim I As Long
Dim J As Long
Dim Num As Long
Dim TargetText As String
TargetText = InputBox("target text?")
J = 1
I = 1
While I > 0
I = InStr(J, ActiveDocument.Range.Text, TargetText)
If I > 0 Then
Num = Num + 1
J = I + 1
End If
Wend
MsgBox Num
End Sub

fumei
09-01-2008, 04:53 AM
Or...




Dim r As Range
Dim j As Long
Set r = ActiveDocument.Range
With r.Find
.Text = InputBox("What word(s)?")
Do While .Execute(Forward:=True) = True
j = j + 1
Loop
End With
MsgBox "Given word(s) was found " & j & " times."



No error trapping on the input string. If there is no string found (because of spelling errors for example) the message will be "0 times"

nim81
10-21-2008, 07:48 AM
Here is a simple way to do it, using the Split() function, where "Needle" is the word/string you want to count:

Dim Haystack As String
Dim Needle As String

Dim a As Variant

a = Split(Haystack, Needle)

MsgBox UBound(a)

RonMcK
10-21-2008, 08:46 AM
Here is a simple way to do it, using the Split() function, where "Needle" is the word/string you want to count:


Dim Haystack As String
Dim Needle As String
Dim a As Variant
a = Split(Haystack, Needle)
MsgBox UBound(a)
Thanks for the suggestion. Not stated in my query is the requirement that the code run on Excel 2004 in Office for Mac where we have no 'split' function.

fumei
10-23-2008, 10:41 AM
There is no Split() in Excel for the Mac? Ai caramba! Why on earth would it be different for the Mac?