Replacement オブジェクト

Microsoft Word Visual Basic リファレンス

Replacement オブジェクト

Find
Replacement
複数のオブジェクト

検索と置換に使用する条件を表します。Replacement オブジェクトのプロパティとメソッドは [検索と置換] ダイアログ ボックスのオプションと対応します。

使い方

Replacement プロパティを使用して Replacement オブジェクトを取得します。次の使用例は、次に単語 "hi" が使用されている箇所を単語 "hello" で置き換えます。

With Selection.Find
    .Text = "hi"
    .ClearFormatting
    .Replacement.Text = "hello"
    .Replacement.ClearFormatting
    .Execute Replace:=wdReplaceOne, Forward:=True
End With
		

書式を検索置換するには、検索する文字列と置換後の文字列の両方に長さ 0 の文字列 ("") を設定し、Execute メソッドの引数 FormatTrue を設定します。次の使用例は、作業中の文書のすべての太字の書式を削除します。この場合、Bold プロパティの値は、Find オブジェクトについては TrueReplacement オブジェクトについては False を設定します。

With ActiveDocument.Content.Find
    .ClearFormatting
    .Font.Bold = True
    .Text = ""
    With .Replacement
        .ClearFormatting
        .Font.Bold = False
        .Text = ""
    End With
    .Execute Format:=True, Replace:=wdReplaceAll
End With