How do I select from insertion point to search result

H

Hambster

In my Word document, I am trying to write VBA code to select all of the text
from the current cursor position to the previous Hard Page Break.

I don't see that there is a way to get the Find method to extend the
selection from the current location to the result of the search, but I am
sure there must be some way to do a similar thing.

Can anyone show a code example to me that would accomplish this purpose,
please.

Thank you in advance.
 
D

Dave Lett

Hi,

You can use something like the following:

Dim oRng As Range
Set oRng = Selection.Range
With Selection.Find
.ClearFormatting
.Text = "^m"
.Forward = False
.Execute
End With
oRng.Start = Selection.Start
oRng.Select

HTH,
Dave
 
H

Hambster

Perfect. Thank you.

Dave Lett said:
Hi,

You can use something like the following:

Dim oRng As Range
Set oRng = Selection.Range
With Selection.Find
.ClearFormatting
.Text = "^m"
.Forward = False
.Execute
End With
oRng.Start = Selection.Start
oRng.Select

HTH,
Dave
 
L

Larry

Just for the heck of it, here's another way to do the same thing:

Application.Run "ExtendSelection"

With Selection.Find
.ClearFormatting
.Text = "^m"
.Forward = False
.Execute
End With

'-----------------
'The ExtendSelection can then be turned off with:

' Selection.EscapeKey
 

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