Class Replacement (Word VBA)

The class Replacement represents the replace criteria for a find-and-replace operation. The properties and methods of the Replacement object correspond to the options in the Find and Replace dialog box. To use a Replacement class variable it first needs to be instantiated, for example


Dim rpl as Replacement
Set rpl = Selection.Find.Replacement

ClearFormatting

Removes text and paragraph formatting from the text specified in a replace operation.


Sub ClrFmtgReplace() 
 Dim rngTemp As Range 
 Set rngTemp = ActiveDocument.Content 
 With rngTemp.Find 
 .ClearFormatting 
 .Replacement.ClearFormatting 
 .MatchWholeWord = True 
 .Execute FindText:="Inc.", ReplaceWith:="incorporated", _ 
 Replace:=wdReplaceAll 
 End With 
End Sub

Font

Returns or sets a Font object that represents the character formatting of the specified object.

To set this property, specify an expression that returns a Font object.


Selection.Find.Replacement.Font =

Frame

Returns a Frame object that represents the frame formatting for the specified style or find-and-replace operation.


Dim frmFrame As Frame
Set frmFrame = Selection.Find.Replacement.Frame

Highlight

True if highlight formatting is applied to the replacement text.

Can return or be set to True, False, or wdUndefined.


Dim rngTemp As Range 
 
Set rngTemp = ActiveDocument.Range(Start:=0, End:=0) 
With rngTemp.Find 
 .ClearFormatting 
 .Highlight = True 
 With .Replacement 
 .ClearFormatting 
 .Highlight = False 
 End With 
 .Execute Replace:=wdReplaceAll, Forward:=True, FindText:="", _ 
 ReplaceWith:="", Format:=True 
End With

LanguageID

Returns or sets a WdLanguageID constant that represents the language for the specified range. Here you can find possible values for WdLanguageID.

Some of the WdLanguageID constants may not be available to you, depending on the language support (U.S. English, for example) that you've selected or installed.


Selection.Find.Replacement.LanguageID = wdAfrikaans

LanguageIDFarEast

Returns or sets an East Asian language for the specified replacement. Here you can find possible values for WdLanguageID.

This is the recommended way to return or set the language of East Asian text in a document created in an East Asian version of Microsoft Word.


Selection.LanguageIDFarEast = wdKorean

NoProofing

True if Microsoft Word replaces text that the spelling and grammar checker ignores.


Selection.Find.Replacement.NoProofing =

ParagraphFormat

Returns or sets a ParagraphFormat object that represents the paragraph settings for the specified replacement operation.


With ActiveDocument.Content.Find 
 .ClearFormatting 
 .ParagraphFormat.Space2 
 .Replacement.ClearFormatting 
 .Replacement.ParagraphFormat.Space15 
 .Execute FindText:="", ReplaceWith:="", _ 
 Replace:=wdReplaceAll 
End With

Style

Returns or sets the style for the specified style. To set this property, specify the local name of the style, an integer, a WdBuiltinStyle constant, or an style that represents the style. Read/write Variant.

When you return the style for a range that includes more than one style, only the first character or paragraph style is returned.


Selection.Find.Replacement.Style =

Text

Returns or sets the text to replace.


Set myRange = ActiveDocument.Content 
With myRange.Find 
 .ClearFormatting 
 .Replacement.ClearFormatting 
 .Text = "Hello" 
 .Replacement.Text = "Goodbye" 
 .Execute Replace:=wdReplaceAll 
End With