Mondays are tough

D

Dr. M

This is a dumb question obviously [it's Monday] but how do I determine if my
cursor is at the last line of the document, or end of file?

Thank you so much.
 
J

Jonathan West

Dr. M said:
This is a dumb question obviously [it's Monday] but how do I determine if
my
cursor is at the last line of the document, or end of file?

Thank you so much.


Not a dumb question. Others have asked it before you. Take a look here

Determine whether the insertion point is located at the end of a document
http://www.word.mvps.org/FAQs/MacrosVBA/SelectionAtEndOfDoc.htm


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Dr. M

Thank you for your response. How to determine if the line I'm on is the last
line. Here is the code I have so far, right now I have to manually put an X
in the first line to get it to quit, when I would rather have it just check
to see if it's on the last line or not of the entire document:

Sub line99()
Do While Selection <> "X"
'Test character for dash in position one
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If Selection <> "-" Then
'Line backspace
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.TypeBackspace
End If
Loop

End Sub
 
H

Helmut Weber

Hi

like this, unless someone shows me, what is wrong:

Public Function Lastline() As Boolean
Dim rngTmp As Range
Set rngTmp = Selection.Range
rngTmp.End = Selection.Range.End
Selection.MoveDown
If Selection.Range.End > rngTmp.End Then
Lastline = False
Else
Lastline = True
End If
End Function

Sub testLast()
MsgBox Lastline
End Sub

The insertion point doesn't move,
if it is in the last line.


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jonathan West

It can be simpler than that. This will do the trick

Public Function Lastline() As Boolean
Lastline = ActiveDocument.Range.End = ActiveDocument.Bookmarks("\Line").End
End Function

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
H

Helmut Weber

Hi Jonathan,

I get an error:

"Function call of left-hand side of assignment
must return variant or object"

Hmm...



Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
C

Chuck Henrich

Hi Jonathan

Just to clarify:

Lastline = Activedocument.Range.End

will be true if the insertion point is at the end of the document while

Lastline = ActiveDocument.Bookmarks("\Line").End

will be true if the IP is at the end of the current line, yes?

Chuck
 
J

Jonathan West

A correction

Public Function Lastline() As Boolean
Lastline = (ActiveDocument.Range.End -
ActiveDocument.Bookmarks("\Line").End) <= 1
End Function

This function returns true in the following circumstances

- The cursor is an insertion point, positioned somewhere on the last line of
the document, or
- The cursor is a solid selection whose start is somewhere on the last line
of the document.


The key to the function is that if the cursor is on the last line of the
document, the \Line built-in bookmark is marking the whole of the last line,
usually excluding the final paragraph mark. Therefore, you can compare its
End property with the end of the document itself to find out if the cursor
is on the last line.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
H

Helmut Weber

Hi Jonathan,

great. Live and learn.

Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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