'AtEndOfDocument' as published does not allways work, some questions.

  • Thread starter Thomas Lindberg
  • Start date
T

Thomas Lindberg

The AtEndOfDocument VBA code, as published by Microsoft, does not always
work.

Function AtEndOfDocument()
If Selection.Type = wdSelectionIP And Selection.End =
ActiveDocument.Content.End - 1
AtEndOfDocument = True
Else
AtEndOfDocument = False
End If
End Function

The code above does not work if the last End of Paragraph sign (is that the
correct expression??) is selected.

The code below works also if the last End of Paragraph sign is selected.

Function AtEndOfDocument()
If Selection.Type = wdSelectionIP And Selection.End =
ActiveDocument.Content.End - 1 Or _
Selection.Type = wdSelectionNormal And Selection.End =
ActiveDocument.Content.End Then
AtEndOfDocument = True
Else
AtEndOfDocument = False
End If
End Function


Now to the questions:
- Are there any hooks with the improved code?
- How come that this trivial issue never (???) has been discussed before?
- Why is not AtEndOfDocument a VBA built in function as in Visual Basic??
(OK, silly qustion but ..)

Thomas
 
H

Helmut Weber

Hi Thomas,

the example from MS gives the correct answer
to the question: "Is the insertion point right
before the last paragraph mark?"

If the selection is extended,
then the selection.type is not the insertion point.
Maybe it would have been better, not to use
a boolean function, but a function returning
3 values: "yes", "no", "no insertion point".

Your question seems to be: "Is the insertion point
before the last paragraph mark or does the selection
encompass the last paragraph mark?"

Therefore your approach returns true,
if all of the document is selected,
and false, if all but the last paragraph mark is selected.

Furthermore, and rather theoretical, there is no end of anything,
in a way, that you cannot grab the end. It is something abstract.
You cannot isolate the end of a rope and nothing but the end.
You cannot hold the end of a rope in your hand and the end only!

And, the insertion point can only be near the document's end.
Right before the last paragraph mark. But then it is before
the document's end. The next position would already be after
the documents end. Paradox, but true.
- How come that this trivial issue never (???) has been discussed before?

I remember having posted something similar to your code
long time ago plus comments on the possible complications.

On the other hand, it is trivial in a way:

If Selection.Range.End = ActiveDocument.Range.End Then

The selection must be extended and includes the last paragraph.
- Why is not AtEndOfDocument a VBA built in function as in Visual Basic??
(OK, silly qustion but ..)

Otherwise, there would be one topic less to discuss. ;-)

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
D

Doug Robbins

It's probably that much of an issue because when carrying some operation
that requires that the whole document be processed, there are usually better
ways as in the following:

' Macro to round all numbers in a document
' Macro created 19/7/00 by Doug Robbins


Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[0-9]{1,}.[0-9]{3,}", MatchWildcards: =
True, Wrap:=wdFindContinue, Forward:=True) = True
Selection.Range.Text = Round(Selection.Range.Text, 2)
Loop
End With

The subject has however been discussed in the article "Determine if the
insertion point is located at the end of a

document" at:

http://word.mvps.org/FAQs/MacrosVBA/SelectionAtEndOfDoc.htm




--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
T

Thomas Lindberg

Helmut,

Thanks for the elaborated answer!
And are you the Helmut Weber I once knew in Ladenburg?

Thomas
 
H

Howard Kaikow

Try the following

Public Function SelectionAtEndOfDocument() As Boolean
If Selection.End = Selection.Start Then
SelectionAtEndOfDocument = (.End = ActiveDocument.Content.End -
1)
Else
SelectionAtEndOfDocument = (.End = ActiveDocument.Content.End)
End If
End Function

It is not unusual to fins KB articles that do not give the best, or most
general, answer,
 

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