Find with MatchWildcards = TRUE is skipping consecutive match in a selected string

M

me

when I try to fing using MatchWildcards opening word window its working
fine but If I do the same through Code, some cases its failing.
Suppose if I find with a wild card "A*[.|;]" Suppose it finds a
selction "An Apple.", when I call the Find->Execute then I should get
"Apple.". This behavior is normal in Word window but When I do it
through Code its failing,in the sense its skipping the current
selection and Searching after the previously selected Selection. How to
mimic the exact word behavior??
Whats going wrong in my case???
 
G

Greg

I will take a stab.

Without some additional manipulation of the range, when a VBA finds a
string like "An Apple" on the first execute it automatically collapses
to the end of the found range before looking for the next instance. To
see the same results as the standard find dialog, you have to
manipulate the found range and collapse it immediately after the first
character found. Consider this example:
Sub ScratchMacro()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "A*[.;]"
.MatchWildcards = True
While .Execute
i = i + 1
With oRng
.Select
.MoveStart Unit:=wdCharacter, Count:=1 'Stet out
.Collapse wdCollapseStart 'Set out
End With
Wend
End With
MsgBox "Expression found " & i & " times."
End Sub


Step through the code as shown and you will find both An Apple. and
Apple.

Stet out the lines indicated and you will only find An Apple.
 

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