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.
--
eugene

Re: moving the cursor to find location by Greg

Greg
Tue Feb 13 15:34:03 CST 2007

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



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


eugene wrote:
> 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.



Re: moving the cursor to find location by eugene

eugene
Tue Feb 13 15:52:01 CST 2007

Greg,
Thanks again. Works as advertised.

--
eugene


"Greg Maxey" wrote:

> 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
>
>
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
>
> eugene wrote:
> > 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.
>
>
>