Find オブジェクト

Microsoft Word Visual Basic リファレンス

Find オブジェクト

複数のオブジェクト
Find
複数のオブジェクト

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

使い方

Find プロパティを使用して Find オブジェクトを取得します。次の使用例は、単語 "hi" が次に使用されている箇所を検索し、選択します。

With Selection.Find
    .ClearFormatting
    .Text = "hi"
    .Execute Forward:=True
End With
		

次の使用例は、作業中の文書で単語 "hi" が使用されているすべての箇所を検索し、"hello" に置き換えます。

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="hi", ReplaceWith:="hello", _
    Replace:=wdReplaceAll
		

解説

Selection オブジェクトから Find オブジェクトを取得した場合、検索条件と一致する文字列が見つかると、選択範囲が変更されます。次の使用例は、単語 "blue" が次に使用されている箇所を検索し、選択します。

Selection.Find.Execute FindText:="blue", Forward:=True
		

Range オブジェクトから Find オブジェクトを取得した場合、検索条件と一致する文字列が見つかっても、選択範囲は変更されませんが、Range オブジェクトが再定義されます。次の使用例は、作業中の文書で単語 "blue" が最初に使用されている箇所を検索します。文書内で "blue" が見つかった場合、myRange が再定義され、太字の書式が "blue" に適用されます。

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", Forward:=True
If myRange.Find.Found = True Then myRange.Bold = True