Results 1 to 4 of 4
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Easy way to find end of document

    You all have given me great help throughout the years, including recently. This time, I want to share an answer rather than a question. I tried various methods for finding when I had reached the end of a document. Somehow, there were always hangups. Then I found a very simple method. I added an unlikely character, a close parenthesis ")", to the end of my document, and then created a "Do Until" loop. Here is is my code. Enjoy!!

    Code:
    Public Sub Macro1()
        Selection.EndKey Unit:=wdStory
        Selection.TypeParagraph
        Selection.TypeText Text:=")"
        Selection.HomeKey Unit:=wdStory
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    
       Do Until Selection.Text = ")"
    '  PUT YOUR CODE  HERE FOR WHAT YOU WANT TO DO UNTIL YOU REACH THE END
    '  PUT CODE HERE TO MOVE TO THE NEXT PARAGRAPH. IF YOU ARE DELETING THINGS POSSIBLY, YOU MAY NOT NEED THIS UNLESS YOUR CONDITION IS NOT MET.
    '  ALSO, YOU MAY WANT TO PUT CODE IN TO TEST IF YOU ARE IN A TABLE (
    Code:
    If Selection.Information(wdWithInTable) Then
    ) AS THAT MAY AFFECT YOUR NAVIGATION THROUGH THE DOCUMENT. Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend Loop Selection.Paragraphs(1).Range.Delete End Sub

  2. #2
    3 Star Lounger
    Join Date
    Jan 2011
    Posts
    284
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Easier: use Ctrl+End
    For the beginning Ctrl+Home.
    Works in all programmes.....

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe my description wasn't clear. If you need to do something until you get to the end of the document, this is a good way to do that.

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Doesn't this assume that there is not another right paren in the document?

    Try this function:

    Code:
    Function IsEndOfDoc () as Boolean
        Dim rng As Range
        Set rng = ActiveDocument.Range
        rng.Collapse wdCollapseEnd
        If Selection.Range.InRange(rng) Then
            IsEndOfDoc = True
        Else
            IsEndOfDoc = False
        End If
    End Function
    Last edited by richardbarrett; 2015-04-16 at 20:50.

Posting Permissions

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