返回表示查找操作的找到文本或替换文本的 TextRange 对象。 此为只读属性。
语法
表达式。FoundTextRange
表达式 表示 FindReplace 对象的变量。
返回值
TextRange
备注
由 FoundTextRange 属性返回的实际 TextRange 对象由 ReplaceScope 属性的值确定。 下表列出了这些属性的相应值 (另请参阅 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
以下示例用单词"strange"替换每个示例中的单词"bizarre",并对所替换的文字应用倾斜和加粗格式。
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 支持和反馈,获取有关如何接收支持和提供反馈的指南。