Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-20-2021, 11:30 AM
Stephen Ray Stephen Ray is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Advanced Beginner
Loop Until End of Document
 
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop Until End of Document How to loop once through document PrincessApril Word VBA 4 04-12-2020 06:06 AM
Loop Until End of Document Loop thru document and create bookmarks jeffreybrown Word VBA 3 09-22-2018 06:04 AM
Loop true all opned word document elbartje Word VBA 1 06-12-2017 05:27 PM
VB for Word - Need macro to loop each line in a document NDeane Word VBA 5 12-22-2015 08:12 PM
Loop Until End of Document Loop through files and Copy Table Row and Paste into Different Document spiderman1369 Word VBA 2 10-15-2014 08:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft