Consulting

Results 1 to 12 of 12

Thread: Solved: Determine whether the selection reached end of the document

  1. #1

    Question Solved: Determine whether the selection reached end of the document

    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.

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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 :-)

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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)

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    1
    ?

    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)

  7. #7
    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.

  8. #8
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Great solution. I was wondering about that just now. But now I found my answers...

    Thx to all who contribute....

  9. #9
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by hylw
    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.
    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

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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)

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Me? Funny?

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Me? Funny?
    You're right...I must had someone else in mind! sorry...
    _________
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •