search result in a range

A

Andrew V

I'm wondering about the use of myRange.Find as opposed to Selection.Find. Is
there some way I can use the former and return/determine the range of its
result? Selection.Find highlights (selects) the result, but I can find no
equivalent action for myRange.Find.

Thanks,
Andrew
 
J

Jean-Guy Marcil

Andrew V was telling us:
Andrew V nous racontait que :
I'm wondering about the use of myRange.Find as opposed to
Selection.Find. Is there some way I can use the former and
return/determine the range of its result? Selection.Find highlights
(selects) the result, but I can find no equivalent action for
myRange.Find.

Try something like this:

'_______________________________________
Sub SelectRangeFind()

Dim myRange As Range

Set myRange = ActiveDocument.Range

With myRange.Find
.Text = "Some text"
.Execute
If .Found Then
With .Parent
.Select
End With
End If
End With

End Sub
'_______________________________________

In fact, .Parent refers to the found range.

So you can put anything you would normally do with a range between the
With .Parent

End With

as in:

With .Parent
.Bold = True
.Font.Name = "Arial"
.Characters(1).Font.Color = wdColorBlue
End With

You do not even have to select the found result.
And you can use a loop to apply those changes to all found items:

'_______________________________________
Sub SelectRangeFind()

Dim myRange As Range

Set myRange = ActiveDocument.Range

With myRange.Find
.Text = "Some text"
Do While .Execute
With .Parent
.Bold = True
.Font.Name = "Arial"
.Characters(1).Font.Color = wdColorBlue
End With
Loop
End With

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top