Getting Range of Next Line without selecting it

A

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
 
T

Tony Jollans

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
 
A

Alan

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
 

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