Discussion:
Getting Range of Next Line without selecting it
(too old to reply)
Alan
2008-12-20 03:11:47 UTC
Permalink
I want to get the Range of the "next" line (one down from current
cursor position), so I can determine if it is empty, and select it if
it is not empty.

I wrote the following code, but I am having two problems: It sets
the Range start properly only when the current line is selected (I
don't want to require this). Also, I am not sure how to find the
value of the end of the next line, so I know how to set the end of the
range.

Can anyone point me in the right direction? Thanks, Alan

Function GetNextLineRange() As Range
Dim Width As Long
Set GetNextLineRange = Selection.Range
Width = GetNextLineRange.End - GetNextLineRange.Start
Set GetNextLineRange = ActiveDocument.Range
(Start:=Selection.Range.End, End:=Selection.Range.End + Width)
MsgBox ("GetNextLineRange = " & GetNextLineRange.Text)
End Function

Sub TestGetNextLineRange()
GetNextLineRange
End Sub
Tony Jollans
2008-12-20 17:02:25 UTC
Permalink
An empty line is not a Word construct, so I presume you really mean next
paragraph.

Checking the next paragraph is straightforward

Set R = Selection.Paragraphs(Selection.Paragraphs.Count).Next.Range
If R.Characters.Count > 1 Then R.Select

If you are really talking about lines rather than paragraphs, it's more
involved. If the current line is the last of a paragraph then the next line
may be an empty paragraph, in which case you want to do nothing; in all
other cases you want to select the next line. Something like this should do
that:

Set R = Selection.Bookmarks("\Line").Range.Characters.Last
If R = vbCr Then If R.Characters.Last.Next = vbCr Then Exit Sub
Selection.EndKey wdLine '
Selection.MoveRight wdCharacter, 1 '.MoveDown wdLine, 1
Selection.Bookmarks("\Line").Range.Select
--
Enjoy,
Tony

www.WordArticles.com
Post by Alan
I want to get the Range of the "next" line (one down from current
cursor position), so I can determine if it is empty, and select it if
it is not empty.
I wrote the following code, but I am having two problems: It sets
the Range start properly only when the current line is selected (I
don't want to require this). Also, I am not sure how to find the
value of the end of the next line, so I know how to set the end of the
range.
Can anyone point me in the right direction? Thanks, Alan
Function GetNextLineRange() As Range
Dim Width As Long
Set GetNextLineRange = Selection.Range
Width = GetNextLineRange.End - GetNextLineRange.Start
Set GetNextLineRange = ActiveDocument.Range
(Start:=Selection.Range.End, End:=Selection.Range.End + Width)
MsgBox ("GetNextLineRange = " & GetNextLineRange.Text)
End Function
Sub TestGetNextLineRange()
GetNextLineRange
End Sub
Alan
2008-12-21 01:24:17 UTC
Permalink
Tony,

Thanks for your reply. . . . I meant paragraphs throughout. I
came up with a longer solution, but it works:

Function BlankNextPara() As Boolean
Dim rng As Range
Dim MyLine As String
'Get range of current selection
Set rng = Selection.Range
'Move range start to next paragraph
rng.Move Unit:=wdParagraph, Count:=1
'Move range end to end of paragraph
rng.MoveEnd Unit:=wdParagraph, Count:=1
' Get the text in range
MyLine = Trim(rng.Text)
' Set the function return value
If Len(MyLine) <= 1 Then
BlankNextPara = True
Else
BlankNextPara = False
End If
End Function

--- Alan

Continue reading on narkive:
Loading...