moving the cursor to find location

E

eugene

Hi,

How does one move the cursor to the beginning of a "find" location (in a
macro)?

I would like to select all the text from a point determined by a "find" back
to the beginning of the document. It seems that when I do find, the cursor
stays in its original location. How can I tell the cursor to move to the
"find" location?

I have tried the following in With .find ... While .execute:
Selection.MoveRight Unit:=wdCharacter, count:=1
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
but the cursor seems not to go down to the "find" location so only the first
character of the document is selected even though there are clear indications
that the loop is finding the correct number of items specified.
 
G

Greg Maxey

Take this sample text:
Four score and seven years ago our fathers brought forth on this continent a
new nation.


Run this code:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "fathers"
.Execute
If .Found Then
oRng.End = oRng.Start 'or delete this line if you want to include the
found text in the selection
oRng.Start = ActiveDocument.Range.Start
oRng.Select
End If
End With
End Sub
 

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