PDA

View Full Version : Solved: Determine whether the selection reached end of the document



hylw
06-29-2005, 09:40 PM
Hi, all :hi:

I would like to know how to determine the selection reached end of the file. Can anybody show me the way, please? Thanks.

Killian
06-30-2005, 01:57 AM
Hi there,
here's some code that looks at the selection.type and the selection and Activedocument content End properties to find out what's going on. You should be able to test for most circumstances with variants of thisIf Selection.Type = wdSelectionIP And Selection.End = ActiveDocument.Content.End - 1 Then
MsgBox "Cursor is the end of the document"
ElseIf Selection.Type = wdSelectionNormal And Selection.End = ActiveDocument.Content.End Then
MsgBox "Selected range includes final paragrph mark"
End If

fumei
06-30-2005, 08:43 AM
An possible alternative:Sub AtEnd()
Select Case (ActiveDocument.Range.End - 1) _
- Selection.Range.End
Case 0
MsgBox "Selection is at end of document, " & _
"and DOES NOT include the final paragraph mark."
Case -1
MsgBox "Selection is at end of document," & _
" but DOES included final paragraph mark."
Case Else
If Selection.Range.End _
- Selection.Range.Start > 1 Then
MsgBox "Selection is Extended, NOT " & _
"at end of the document." & _
" and covers " & (Selection.Range.End * 100) _
/ ActiveDocument.Range.End & _
" percent of the document."
Else
MsgBox "Selection is NOT Extended, but is " & _
"also NOT at the end of the document."
End If
End Select
End Sub
As Killian points out, there is a difference between do a Ctrl-End, which moves the Selection to the end of the document and selection that character.

Ctrl-End (the "end" of the document.) does NOT include the final paragraph mark. This can be a crucial issue, especially when dealing with changes of Styles.

If you want to include the paragraph mark when moving, or actually selecting the end of a document.Selection.EndKey Unit:=wdStory
Selection.MoveEnd Unit:=wdLine, Count:=1 moves the selection to the end of the document, AND selects the final paragraph mark.

The above code tests for the range integer of the selection. There are five possible scenarios.

The selection is a point:
a) at the end , but NOT including the final paragraph
b) at the end, but DOES include the final paragraph mark
The selection is extended (not just a point)
c) the extended selection go to the end, but does NOT include the final paragraph mark
d) the extended selection goes to the end and DOES include the final paragraph mark
e) the extended selection does NOT go to the end. Obviously this means the final paragraph is not included.

Just as a toss in, the procedures with scenario e), also informs on the percentage of the document that IS selected. This may be a trivial piece of information, or not.

MOS MASTER
06-30-2005, 10:34 AM
Hi, :yes

Ah the Selection Object..where will we start. (Kidding Gerry & Killian did a nice Job as usual)

BTW nice explanation Gerry!

The problem here is that a selection has many types so it's difficult to give you the right code unless you tell us exactly what you wish to achieve.

A selection can span more then one character but could also just span 1 character where start and end are the same possition (Flashing cursor) AKA wdSelectionIP

Selection could have special modes (block) do we need to address that...

Here's one more just to trow in a extra example:
Sub SelectionEnd()
If Selection.Type = wdSelectionNormal And _
Selection.End = ActiveDocument.Content.End Then

MsgBox "Selection includes final paragraph mark"

ElseIf Selection.Type = wdSelectionIP And _
Selection.End = ActiveDocument.Content.End - 1 Then

MsgBox "Selection is flashing at end of document"
End If
End Sub


We could honestly talk about this subject for many pages but I think you should point out exactly what en when you wanna use this and how it should work..

Later..:whistle:

fumei
06-30-2005, 10:58 AM
1

MOS MASTER
06-30-2005, 11:06 AM
1
?

Are you trying to be funny now? :yes

hylw
06-30-2005, 07:09 PM
Hi, all:rotlaugh:

Thanks to all expert who willing giving me the advise and comment. The coding provided do help me a lot, i really appreciate your help. :friends:

Thanks again. :beerchug:

sheeeng
06-30-2005, 07:54 PM
Great solution. I was wondering about that just now. But now I found my answers...

Thx to all who contribute....

Howard Kaikow
07-01-2005, 07:38 AM
Hi, all :hi:

I would like to know how to determine the selection reached end of the file. Can anybody show me the way, please? Thanks.

It's better to use the Range object, but if you use Selection object, then:



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

MOS MASTER
07-01-2005, 09:33 AM
Glad we could help! :beerchug:

fumei
07-04-2005, 08:17 AM
Me? Funny?

MOS MASTER
07-04-2005, 08:22 AM
Me? Funny?
You're right...I must had someone else in mind! :rofl: sorry...:rotlaugh: