Publisher) (FindReplace.FoundTextRange 屬性

傳回 TextRange 物件,代表尋找作業找到的文字或取代的文字。 唯讀。

語法

運算式。FoundTextRange

運算式 代表 FindReplace 物件的變數。

傳回值

TextRange

註解

ReplaceScope 屬性值取決於 FoundTextRange 屬性所傳回的實際 TextRange 物件。 下表列出這些屬性的對應值 (另請參閱 PbReplaceScope 列舉) 。

當 ReplaceScope = FoundTextRange =
pbReplaceScopeAll Empty
pbReplaceScopeNone 尋找文字範圍
pbReplaceScopeOne 取代文字範圍

ReplaceScope 設為 pbReplaceScopeAll 、 FoundTextRange 屬性為空白。 任何嘗試存取它都會傳回「拒絕存取」。操作搜尋文字之文字範圍的方法是將 ReplaceScope 屬性設定為 pbReplaceScopeNone 或 pbReplaceScopeOne ,並針對找到的每個項目存取所搜尋或取代文字的文字範圍。

範例

當 ReplaceScope 設為 pbReplaceScopeNone 時、 FoundTextRange 會傳回搜尋文字的文字範圍。 下列範例說明如何尋找文字範圍的字型屬性可以存取 ReplaceScope 設為 pbReplaceScopeNone 時。

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

ReplaceScope 設為 pbReplaceScopeOne 、 時已被取代之搜尋文字的文字範圍。 因此, FoundTextRange 屬性會傳回取代文字的文字範圍。 下列範例會示範如何取代的文字範圍的字型屬性可以存取 ReplaceScope 設為 pbReplaceScopeOne 時。

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

本範例會將 "bizarre" 這個字取代為 "strange",並將斜體格式和粗體格式套用至取代文字。

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

本範例會尋找所有出現的 "important",並套用以斜體格式。

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 支援與意見反應。