wdFindStop doesn't stop in Range.Find procedure

F

Fr?d?ric Laurent

Hello,

The only text in my document is :
<ABCDE><FGHIJK>lmno<PQRSTU><VWXYZ>

The purpose of this function is to get only the XML tags of the
selection.

If I manually select (highlight) <FGHIJK>mno<PQRSTU> and run this
code, I get <FGHIJK><PQRSTU><VWXYZ> when I was only expecting
<FGHIJK><PQRSTU>.

What's wrong with this??? It seems that the wdFindStop doesn't do its
job and the function searches to the end of the document (I tried with
much longer lines in the document: same problem)


Set MyRange = Selection.Range
With MyRange.Find
.Text = "\<*\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While MyRange.Find.Execute
sXmlTagOnly = sXmlTagOnly & MyRange.Text
Loop

Thank you for your help!

Frédéric Laurent
 
J

Jay Freedman

Hi Frédéric,

The problem you're running into is that every time you call .Execute
and it returns True, it has redefined myRange to cover the found text.
For example, after the first call, myRange covers <FGHIJK>. This means
that the next call will start searching from the end of that area.
Since myRange no longer extends beyond that first area, the trigger
point for .Wrap is the end of the document, not the end of the
original range.

To fix this, change the Do While statement to

Do While myRange.Find.Execute And _
myRange.InRange(Selection.Range)

When the found text lies outside the Selection, the loop will stop.

--
Regards,
Jay Freedman
Microsoft Word MVP


http://www.gmayor.com/replace_using_wildcards.htm
 
J

Jean-Guy Marcil

Fr?d?ric Laurent was telling us:
Fr?d?ric Laurent nous racontait que :
Hello,

The only text in my document is :
<ABCDE><FGHIJK>lmno<PQRSTU><VWXYZ>

The purpose of this function is to get only the XML tags of the
selection.

If I manually select (highlight) <FGHIJK>mno<PQRSTU> and run this
code, I get <FGHIJK><PQRSTU><VWXYZ> when I was only expecting
<FGHIJK><PQRSTU>.

What's wrong with this??? It seems that the wdFindStop doesn't do its
job and the function searches to the end of the document (I tried with
much longer lines in the document: same problem)

I was going to suggest something similar to Jay's approach. Here it is
anyway, it might help you later with a different problem. It is based on the
Duplicate property:

'_______________________________________
Dim MyRange As Range

Set MyRange = Selection.Range

With MyRange.Duplicate
With .Find
.Text = "\<*\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While .Find.Execute And .InRange(MyRange)
sXmlTagOnly = sXmlTagOnly & .Text
Loop
End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
F

Fr?d?ric Laurent

Thank you very much to both of you !
Jean-Guy, I appreciate you introduced this new (to me at least) method
..Duplicate. It will be usefull to do other things!

FL
 
J

Jean-Guy Marcil

Fr?d?ric Laurent was telling us:
Fr?d?ric Laurent nous racontait que :
Thank you very much to both of you !
Jean-Guy, I appreciate you introduced this new (to me at least) method
.Duplicate. It will be usefull to do other things!

Glad to have been able to help!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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