Find: had to continually reset range??

E

Ed

I was playing with a wildcard search macro. The first thing I did was
record it - call up Find, put in my search terms, and click Find Next until
it told me there were no more. When I looked at the code, .Wrap was
wdFindContinue, and each successive search to go to the next one was simply
Selection.Find.Execute.

I used that code to create a macro that would search for specific terms
based on wildcards and write the found terms to an array. I used ranges
instead of Selection. But when I tried to run it through, it kept looping
back to the first term, rather than stopping at the last term found. To fix
that, I had to do two things: change .Wrap to wdFindStop, and reset the
search range to begin at the end of the last term found to ensure I could
not search them.

Is this a range resetting a normal procedure? What did I do that made it
necessary, and what could I have done different to avoid that? Or does it
really matter?

Ed

Sub WildCardSearch()



Dim rngDoc As Range

Dim rngFnd As Range

Dim doc As Document

Dim str As String

Dim cnt As Long, x As Long

Dim ary() As String



Set doc = ActiveDocument

cnt = 0



' This is a string I put in the document to test the macro.

' A6, B10, C11, D2, and P77 are valid responses;

' F6, AA11, and B123 are invalid.

str = "They are A6, B10, C11, D2, F6, P77, AA11, B123"



Set rngDoc = doc.Content

rngDoc.Text = str



' I set a second range object up as my search range.

Set rngFnd = rngDoc.Duplicate



Do

cnt = cnt + 1

ReDim Preserve ary(cnt)

rngFnd.Find.ClearFormatting

With rngFnd.Find

.Text = "<[ABCDP]{1}[0-9]{1,2}>"

.Replacement.Text = ""

.Forward = True

' .Wrap = wdFindContinue

' I had to change .Wrap to make the macro work;

' otherwise it started over again at the first term

.Wrap = wdFindStop

.Format = False

.MatchCase = False

.MatchWholeWord = False

.MatchAllWordForms = False

.MatchSoundsLike = False

.MatchWildcards = True

End With



If rngFnd.Find.Execute = False Then

cnt = cnt - 1

Exit Do

Else

ary(cnt) = rngFnd.Text

Debug.Print cnt

Debug.Print ary(cnt)

' Here's where I reset my search range;

' otherwise the search would find the last

' term, then start again and find the first term.

rngFnd.Collapse wdCollapseEnd

rngFnd.SetRange rngFnd.Start, rngDoc.End - 1

End If

Loop



For x = 1 To cnt

Debug.Print ary(x)

Next x

End Sub
 
H

Helmut Weber

Hi Ed,

IMHO, you don't need more than this,
in fact, one could code it even shorter,
but that's a matter of readability
and ease of maintanance.

I've changed variable names to the way I'm used to
for short samples of code.

Sub Test00987()
Dim rDcm As Range
Dim lCnt As Long
Set rDcm = ActiveDocument.Range
lCnt = 0
' get the number of found expression
' in order to dimension the array
With rDcm.Find
.Text = "<[ABCDP]{1}[0-9]{1,2}>"
.MatchWildcards = True
While .Execute
lCnt = lCnt + 1
Wend
End With
ReDim sArr(1 To lCnt)
Set rDcm = ActiveDocument.Range ' <<<<<<<<
lCnt = 0
With rDcm.Find
.Text = "<[ABCDP]{1}[0-9]{1,2}>"
.MatchWildcards = True
While .Execute
lCnt = lCnt + 1
sArr(lCnt) = rDcm.Text
Debug.Print Format(lCnt, "000 =") & sArr(lCnt)
Wend
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
E

Ed

Hi, Helmut. That looks a lot cleaner. Run the Find once to get the number
of terms, then run it again to actually get the terms. Saves steps by not
ReDimming the array every time or resetting the range every time.

I knew you didn't need a lot of the other stuff in the Find, but I don't
always know what will make a difference so I generally leave it in. I
thought you would need the .Wrap, though - but your method makes it an
extra. So the steps to evaluate all the other Find criteria are also saved.

Thank you!
Ed
 
H

Helmut Weber

Hi Ed,
I knew you didn't need a lot of the other stuff in the Find, but I don't
always know what will make a difference so I generally leave it in.

it was someone who outed himself as
a newbee in the german groups,
but insisted, by creating a new range.find object
all it's properties were default values.
Whatever the default values are.
They seem to be, what we expect.
In the end, I had to give in.
(Very clever young man!)

Try it out.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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