Word VBA Range.Find with Wildcard

I know that if I use Selection.Find with a wildcard, and if Selection.Find.Execute is true, then the text that matches the wildcard is Selected.

If I use Range.Find, and Range,Find.Execute is true, where is the equivalent matching result?

The Microsoft documentation for Range.Find and Find.Execute do not specify where it is.

Answer
Answer

When you use a Range object and call its .Find.Execute method (with or without a wildcard), if a match is found then the .Start and .End of the Range object are set to the beginning and end of the matched text -- in effect, the Range object moves to the matched text. The string Range.Text can then be manipulated or assigned to a variable.

This has an important implication if you're calling .Find.Execute in a loop. Just before the Next (or Wend or Loop) statement, you must collapse the Range object to its end (call Range.Collapse wdCollapseEnd) so the next iteration will start where the current match ended. If you forget to do that, you get an infinite loop as each .Find.Execute just finds the current match.

_____________________________
https://jay-freedman.info

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated June 12, 2024 Views 1,676 Applies to: