• Easy way to find end of document

    Author
    Topic
    #499421

    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[/CODE]

    Viewing 0 reply threads
    Author
    Replies
    • #1499423

      Easier: use Ctrl+End
      For the beginning Ctrl+Home.
      Works in all programmes…..

      • #1499619

        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.

        • #1500115

          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
          
    Viewing 0 reply threads
    Reply To: Easy way to find end of document

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: