Finding text

I

IndyPaul

I'm recording a macro to use across multiple documents. One task of the
macro is to select a text string in the document and then take actions with
multiple occurances of that string. The "non-programmed" way of doing this
is to select the string, copy it, open the find box, and paste the string
into the find window and click "Find Next."

When I record these actions, the actual selected text gets saved into the
macro as follows:
With Selection.Find
.Text = " Selected text"

The next time I run the macro, in another file, with a different text
string, the macro looks for the text string from the recorded file.

How can I search for text I select from the document I am actually editing?

Thanks
 
G

Graham Mayor

Do you mean like

Dim sText As String
Dim oRng As Range
With Selection
sText = .Text
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=sText, MatchWildcards:=True)
Set oRng = Selection.Range
'do what you want with the found text here e.g.
MsgBox oRng.Text
Loop
End With
End With

The macro will find each occurrence of the selected text (sText) in the
current story range

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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