PDA

View Full Version : Solved: how to creating a range relative to the inertion point



marcelma
11-14-2010, 11:02 AM
Hello,

is there any convenient way of creating a range relative to the present cursor position? For example a range that encompasses 3 whole paragraphs, starting at the beginning of the paragraph before the one in which the selection point is located and ending at the paragraph mark of the paragraph following the one in which the selection point is located.

I know how to select these 3 paragraphs and then creating the range from the selection, but is there any way of creating it without moving the selection point and then finding back to it?

Thanks a lot in advance,
Marcel

gmaxey
11-14-2010, 11:33 AM
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = Selection.Paragraphs(1).Range
On Error Resume Next
oRng.MoveStart wdParagraph, -1
oRng.MoveEnd wdParagraph, 1
oRng.Select
End Sub

marcelma
11-14-2010, 12:18 PM
Thanks Greg, I am thrilled! :thumb

One question: Why did you insert the "On Error Resume Next" line?

greetings,
Marcel

gmaxey
11-14-2010, 01:31 PM
Well I didn't test but I thought if the IP was in the first or last paragraph that it might throw and error.

fumei
11-15-2010, 11:12 AM
It does not throw an error.

gmaxey
11-15-2010, 11:33 AM
It does not throw an error.

Thanks!

fumei
11-15-2010, 11:36 AM
You kind of think it should, but it does not.

gmaxey
11-15-2010, 01:10 PM
Gerry,

I had already closed the test document (without saving) before I posted. Then looking at the post I thought I better add the error statement just in case.

fumei
11-15-2010, 03:23 PM
Greg, it is never wrong to have an error statement. The only thing that can go wrong is if you have a general Resume Next and you get an error that truly screws things. At this level here, that is not going to happen.