O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Searching for Formatting

The following example illustrates how to search for formatting (or formatted text). In this case, I search for any italicized text that is centered. As you can see, this is done by setting the corresponding formatting properties of the Find object (Font and ParagraphFormat) and then setting the Format property of the Find object to True. I must also not forget to clear the formatting first, so that only my formatting will be in effect. (By setting the Text property of the Find object to the empty string, all text is found.)

	Selection.Find.ClearFormatting
	Selection.Find.Font.Italic = True
	Selection.Find.ParagraphFormat.Alignment = _
	   wdAlignParagraphCenter
	Selection.Find.Replacement.ClearFormatting
	With Selection.Find
	   .Text = ""
	   .Replacement.Text = ""
	   .Forward = True
	   .Wrap = wdFindContinue
	   .Format = True
	   .MatchCase = False
	   .MatchWholeWord = False
	   .MatchWildcards = False
	   .MatchSoundsLike = False
	   .MatchAllWordForms = False
	End With
	Selection.Find.Execute

The following example searches for a particular style, in this case the Heading 1 style:

	Selection.Find.ClearFormatting
	Selection.Find.Style = _
	   ActiveDocument.Styles("Heading 1")
	With Selection.Find
	   .Text = ""
	   .Replacement.Text = ""
	   .Forward = True
	   .Wrap = wdFindContinue
	   .Format = True
	   .MatchCase = False
	   .MatchWholeWord = False
	   .MatchWildcards = False
	   .MatchSoundsLike = False
	   .MatchAllWordForms = False
	End With
	Selection.Find.Execute

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required