check if insertion point is at the end of a line

A

Associates

Hi,

I was trying to figure out where the insertion point is in VBA. My idea is
to check for two scenarios.

First scenario is if the insertion point is at the end of a line, it would
need to go to the next line and do something

Second scenario is if the insertion point is already at the start of a new
line, do not go to the next line but do something instead.

My question is how to know if the insertion point is at the end of the line
in VBA codes as shown below,

blah blah blah| <-- this is where the insertion point is at (at the end of
the line)

| <-- this is where the insertion point is at (at the start of a new line)

Any help would be greatly appreciated.

Thank you in advance
 
D

Doug Robbins - Word MVP on news.microsoft.com

You would think that

If Selection.Start = ActiveDocument.Bookmarks("\line").Range.End Then
MsgBox "Insertion Point is at end of line."
ElseIf Selection.Start = ActiveDocument.Bookmarks("\line").Range.Start Then
MsgBox "Insertion Point is at the start of the line."
Else
MsgBox "Insertion Point is neither at the start nor the end of the
line."
End If

might tell you, but if the selections is at the end of the line (following
the space after the last word on the line, the above code will tell you that
it is at the start of the next line.

If you move the start of the selection so that it includes the space at the
end of the line, the above code tells you that it is neither at the start,
not at the end of the line.

If think that to be sure, you might have to use the
Selection.Information(wdVerticalPositionRelativeToPage) to check the
vertical position of the selection and then move it one character and see if
the Selection.Information(wdVerticalPositionRelativeToPage) changes.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

David Horowitz

Well, taking off from where Doug pointed us, I got what I believe is a fully
functioning subroutine which tells you if the Word cursor is at the start of
a line, at the end of a line, or neither:

Sub StartOrEnd()

If Selection.Start = ActiveDocument.Bookmarks("\line").Range.Start Then
If Selection.Range.Information(wdVerticalPositionRelativeToPage) <>
_
ActiveDocument.Bookmarks("\line").Range.Information(wdVerticalPositionRelativeToPage)
Then
MsgBox "END" ' special case - either used the End key or clicked
with the mouse
Else
MsgBox "START"
End If
ElseIf Selection.Start = ActiveDocument.Bookmarks("\line").Range.End - 1
Then ' need a minus one here
MsgBox "END"
Else
MsgBox "NEITHER"
End If

End Sub

I noticed so many Word weirdnesses here it's hard to keep track of them...
#1 - this assumes your selection is collapsed to an insertion point. If it's
not, this code probably would need modification.
#2 - I noticed the strangest thing - that
Selection.Information(wdVerticalPositionRelativeToPage) returns a slightly
different result than
Selection.Range.Information(wdVerticalPositionRelativeToPage) (note the
..Range property inserted). Selection.Range is what we actually wanted
because it seems to provide more accurate data. How weird is that? I've
always wondered about the status of the Selection object - it seems to be a
Range object yet it's not...
#3 - How's this one -
Selection.Range.Information(wdVerticalPositionRelativeToPage) yields
different results the first time you call it from the second and succeeding
calls. Yes, I mean if you MsgBox its result twice in a row, you get
different results. And it's not because of the MsgBox, because I tried
assigning the value to two separate variables and they get different
results. OK, that's quite weird too I would say.
#4 - When you press the End key in Word while in a line that does NOT have a
paragraph mark at the end of the line, your cursor goes somewhere you can't
get to using the arrow keys. That's what Doug was saying - Word seems to
report that you're at the start of the next line.
#5 - If your cursor is on a blank line (just a paragraph mark), we treat it
as the Start of a line (not the end).
#2 and #3 are weird enough for me...
 
T

Tony Jollans

Before I'd even look at this, I'd want to know (a) what you consider to be a
line, and (b) what you propose to do that is dependent on insertion point.

This is not me being difficult. This is simply that there is an enormous
difference between where the text happens to break for end of line within a
paragraph and where a paragraph (or hard-line-break-ended line) is ended. In
the case of the former, there isn't really any difference between end of one
line and beginning of next for practical purposes, and in the case of the
latter, the check is a really easy one.
 
A

Associates

Thank you all for your replies.

My problem is now fixed. I used the code given by Doug and it worked.

Many Thanks
 

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