The Execute Method

The Execute method of the Find object actually has a number of parameters that allow an alternative syntax for doing a search and replace. The full syntax of the Execute method is:

FindObject.Execute(FindText, MatchCase, MatchWholeWord, _
	MatchWildcards, MatchSoundsLike, MatchAllWordForms, _
	Forward, Wrap, Format, ReplaceWith, Replace)

For instance, the previous search and replace could have been coded as follows:

	Selection.Find.ClearFormatting
	Selection.Find.Replacement.ClearFormatting
	Selection.Find.Execute _
	   FindText:="find", _
	   ReplaceWith:="replace", _
	   Forward:=True, _
	   Wrap:=wdFindContinue, _
	   MatchCase:=False, _
	   MatchWholeWord:=False, _
	   MatchWildcards:=False, _
	   MatchSoundsLike:=False, _
	   MatchAllWordForms:=False, _
	   replace:=wdReplaceAll

This syntax seems to be a bit less readable than the previous one, but you may run across it when reading other code.

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.