Thread: [Solved] Loop Until End of Document
View Single Post
 
Old 02-20-2021, 11:30 AM
Stephen Ray Stephen Ray is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Sep 2018
Location: Kansas
Posts: 34
Stephen Ray is on a distinguished road
Default Loop Until End of Document

Here is a Macro that loops until the end of the document.
The test is this: Selection.Range.End = ActiveDocument.Range.End


This Macro highlights only the Paragraph Mark at the end of a line.


If that highlighted Paragraph Mark is also the last Paragraph Mark in the document, BINGO!


You may see the highlighted Paragraph Mark by turning on the Paragraph Mark:
Home Tab, Click on the Paragraph Mark.


Enjoy!


Sub LoopUntilEndOfDocument()
Selection.HomeKey Unit:=wdStory
counter = 0
Do Until Selection.Range.End = ActiveDocument.Range.End
If counter > 0 Then
Selection.MoveDown Unit:=wdLine, Count:=1
End If

counter = counter + 1
If counter > 49 Then Exit Do 'This prevents an endless loop while testing, You
'will want to increase this or take it out entirely.
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Loop
MsgBox "The loop made " & counter & " repetitions."

End Sub
Reply With Quote