Hi, all
I would like to know how to determine the selection reached end of the file. Can anybody show me the way, please? Thanks.
Hi, all
I would like to know how to determine the selection reached end of the file. Can anybody show me the way, please? Thanks.
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 this[VBA]If 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[/VBA]
K :-)
An possible alternative:[vba]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[/vba]
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.[vba]Selection.EndKey Unit:=wdStory
Selection.MoveEnd Unit:=wdLine, Count:=1[/vba] 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.
Hi,
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:[VBA]
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
[/VBA]
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..
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
1
?Originally Posted by fumei
Are you trying to be funny now?
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Hi, all
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.
Thanks again.
Great solution. I was wondering about that just now. But now I found my answers...
Thx to all who contribute....
It's better to use the Range object, but if you use Selection object, then:Originally Posted by hylw
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
Glad we could help!
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Me? Funny?
You're right...I must had someone else in mind! sorry...Originally Posted by fumei
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)