Can't get recursive Range.Find in Word VBA to work

帅比萌擦擦* 提交于 2019-12-05 16:06:17

Word's Find behavior is very odd.

Among other peculiarities, if your search text is an exact match for the Range's text, then the Wrap option is ignored, and the search range is redefined as per this article:

When the Find object .Execute method determines that the thing to find exactly matches the search range, the search range is dynamically redefined. The new search range starts at the end of the old search range and ends at the end of the document (or targeted storyRange). Processing continues in the redefined range.

That's why the {fizzbuzz} (with the trailing space) works - it's not an exact match.

You'll need to adapt your code to handle:

  1. Range.Text is an exact match for the wildcard search, and/or:
  2. After calling Execute, check that the Range's start is before the expected end.

You can see the Range changes in action by adding a Range.Select statement before and after every Execute call and before and after every Text assignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!