Baffling Find Count Problem

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
 
H

Helmut Weber

Hi David,
the problem with selections as well as with ranges is,
that both are defined anew after a successfull search,
unless it's the whole document.
The pricinple would be like this:
Get the end of the actual selection or range.
If found collapse the range or selection to the end.
Set the end of the range or of the selection to the
original end, which means, expand range or selection
to the before defines point. Beware of replacements,
as they could influence the original end.

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT4.0
 
D

David Yeager

Helmut,

Thanks for your help... but I'm afraid I don't know how I
would actually do what you're suggesting programmatically
(i.e. find the end of the range/selection). Any chance
you could help me out that way?

-Dave
 
M

Mark Tangard

David,

[expression].Collapse wdCollapseEnd

where [expression] is a range or selection object.
 
H

Helmut Weber

Dim oRng As Range
Hi David,
how about that:
(Don't forget to reset Search-Options)

Dim iEnd As Integer ' end of range
Dim iFnd As Integer ' found
Set oRng = Selection.Range
iEnd = oRng.End
With oRng.Find
.Text = "e" ' count number of "e"
While .Execute
iFnd = iFnd + 1
oRng.Collapse direction:=wdCollapseEnd
oRng.End = iEnd
Wend
End With
MsgBox iFnd
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, XP, NT4.0, W98
 

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