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
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