D
David Yeager
I'm trying to count the number of occurences in a given
selection of a given word through VBA. Modifying the code
on the Word MVP FAQ to do so, I have the following
function:
Function CountOccurences(strSearch As String, Optional
WildCardSwitch As Boolean = True) As Integer
' Counts number of times passed string occurs in
selection
Dim iCount As Integer
' Initialize counter
iCount = 0
With Selection.Find
.Text = strSearch
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = WildCardSwitch
Do While .Execute
iCount = iCount + 1
Loop
End With
CountOccurences = iCount
End Function
Pretty straight forward. However, the problem comes when
I'm trying to count the occurences of a word only in a
selected range... the ranges are broken apart by tags
(like SGML). When the first range is selected,
CountOccurences() looks for not only the occurences of the
word in the given range, but all the way through to the
end of the document. When the second range is selected,
CountOccurences(), the same thing happens, but the first
range is ignored. Likewise for the third, fourth, etc. so
that only the last range gives the appropriate count. The
ranges are laid out sequentially in the document. The
call to CountOccurences() is being made in this code:
' Where CurrentTable is a Range variable
CurrentTable.Select
intStepCount = CountOccurences("\<tsstep\>*\<\/tsstep\>")
This is driving me totally batty. Any help or insight
would be terrific.
-Dave
selection of a given word through VBA. Modifying the code
on the Word MVP FAQ to do so, I have the following
function:
Function CountOccurences(strSearch As String, Optional
WildCardSwitch As Boolean = True) As Integer
' Counts number of times passed string occurs in
selection
Dim iCount As Integer
' Initialize counter
iCount = 0
With Selection.Find
.Text = strSearch
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = WildCardSwitch
Do While .Execute
iCount = iCount + 1
Loop
End With
CountOccurences = iCount
End Function
Pretty straight forward. However, the problem comes when
I'm trying to count the occurences of a word only in a
selected range... the ranges are broken apart by tags
(like SGML). When the first range is selected,
CountOccurences() looks for not only the occurences of the
word in the given range, but all the way through to the
end of the document. When the second range is selected,
CountOccurences(), the same thing happens, but the first
range is ignored. Likewise for the third, fourth, etc. so
that only the last range gives the appropriate count. The
ranges are laid out sequentially in the document. The
call to CountOccurences() is being made in this code:
' Where CurrentTable is a Range variable
CurrentTable.Select
intStepCount = CountOccurences("\<tsstep\>*\<\/tsstep\>")
This is driving me totally batty. Any help or insight
would be terrific.
-Dave