FoundTextRange Property [Publisher 2003 VBA Language Reference]
Returns a TextRange object that represents the found text or replaced text of a find operation. Read-only.
expression.FoundTextRange
expression Required. An expression that returns a FindReplace object.
Remarks
The actual TextRange returned by the FoundTextRange property is determined by the value of the pbReplaceScope property. The following table lists the corresponding values of these properties.
for pbReplaceScopeAll | FoundTextRange = Empty |
for pbReplaceScopeNone | FoundTextRange = Find text range |
for pbReplaceScopeOne | FoundTextRange = Replace text range |
When ReplaceScope is set to pbReplaceScopeAll the FoundTextRange is empty. Any attempt to access it will return Access denied. The way to manipulate the text range of the searched text is to set the ReplaceScope to pbReplaceScopeNone or pbReplaceScopeOne and access the text range of the searched or replaced text for each occurrence found.
When ReplaceScope is set to pbReplaceScopeNone, FoundTextRange returns the text range of the searched text. The following example illustrates how the font attributes of the find text range can be accessed when ReplaceScope is set to 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
When ReplaceScope is set to pbReplaceScopeOne the text range of the searched text is replaced. Therefore the FoundTextRange returns the text range of the replacement text. The following example demonstrates how the font attributes of the replaced text range can be accessed when ReplaceScope is set to 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
Examples
This example replaces each example of the word "bizarre" with the word "strange" and applies italics and bold formatting to the replaced text.
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
This example finds all occurrences of the word "important" and applies italics formatting to it.
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
Applies to | FindReplace Object