FindReplace.FoundTextRange プロパティ (発行元)

検索操作で見つかったテキストまたは置き換えられたテキストを表す TextRange オブジェクトを返します。 読み取り専用です。

構文

FoundTextRange

FindReplace オブジェクトを表す変数。

戻り値

TextRange

注釈

FoundTextRange プロパティが返す実際の TextRange オブジェクトは、 ReplaceScope プロパティの値によって決定されます。 次の表に、これらのプロパティの対応する値を示します ( PbReplaceScope 列挙型も参照してください)。

When ReplaceScope = FoundTextRange =
pbReplaceScopeAll Empty
pbReplaceScopeNone 文字列範囲を検索する
pbReplaceScopeOne テキスト範囲を置換する

ReplaceScopepbReplaceScopeAll に設定されると、FoundTextRange プロパティは空になります。 アクセスを試みると、"アクセスが拒否されました" が返されます。検索されたテキストのテキスト範囲を操作する方法は、 ReplaceScope プロパティを pbReplaceScopeNone または pbReplaceScopeOne に設定し、見つかった各出現箇所の検索または置換されたテキストのテキスト範囲にアクセスすることです。

ReplaceScopepbReplaceScopeNone に設定されている場合、 FoundTextRange は検索対象のテキストのテキスト範囲を返します。 次の使用例は、 ReplaceScopepbReplaceScopeNone に設定すると検索のテキスト範囲のフォント属性にアクセスする方法を示しています。

With TextRange.Find 
 .Clear 
 .FindText = "important" 
 .ReplaceScope = pbReplaceScopeNone 
 Do While .Execute = True 
 'The FoundTextRange contains the word "important". 
 If .FoundTextRange.Font.Italic = msoFalse Then 
 .FoundTextRange.Font.Italic = msoTrue 
 End If 
 Loop 
End With

ReplaceScopepbReplaceScopeOne に設定されている場合、検索したテキストのテキスト範囲が置き換えられます。 したがって、 FoundTextRange プロパティは、置換後の文字列のテキスト範囲を返します。 次の使用例は、 ReplaceScopepbReplaceScopeOne に設定すると、置き換えられたテキスト範囲のフォント属性にアクセスする方法を示します。

With Document.Find 
 .Clear 
 .FindText = "important" 
 .ReplaceWithText = "urgent" 
 .ReplaceScope = pbReplaceScopeOne 
 Do While .Execute = True 
 'The FoundTextRange contains the word "urgent". 
 If .FoundTextRange.Font.Bold = msoFalse Then 
 .FoundTextRange.Font.Bold = msoTrue 
 End If 
 Loop 
End With

次の使用例は、"奇怪な" を "奇妙な" に置換し、置換後の文字列に斜体と太字の書式を適用します。

Dim objDocument As Document 
 
Set objDocument = ActiveDocument 
With objDocument.Find 
 .Clear 
 .FindText = "bizarre" 
 .ReplaceWithText = "strange" 
 .ReplaceScope = pbReplaceScopeOne 
 Do While .Execute = True 
 .FoundTextRange.Font.Italic = msoTrue 
 .FoundTextRange.Font.Bold = msoTrue 
 Loop 
End With

次の使用例は、"重要" という語を検索し、そのすべてに斜体の書式を適用します。

Dim objTextRange As TextRange 
 
Set objTextRange = ActiveDocument.Pages(1).Shapes(1).TextFrame.TextRange 
With objTextRange.Find 
 .Clear 
 .FindText = "important" 
 .ReplaceScope = pbReplaceScopeNone 
 Do While .Execute = True 
 .FoundTextRange.Font.Italic = msoTrue 
 Loop 
End With

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。