Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 02-20-2021, 12:45 PM
gmaxey gmaxey is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Steven,


I fail to see what there is to enjoy about running or watching a macro that basically just tells you how many lines are in a document. What am I missing?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 02-20-2021, 02:02 PM
macropod's Avatar
macropod macropod is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Stephen Ray View Post
Here is a Macro that loops until the end of the document.
...
If that highlighted Paragraph Mark is also the last Paragraph Mark in the document, BINGO!
What does that achieve the you don't get with:
Code:
Sub ParagraphCount()
MsgBox ActiveDocument.Paragraphs.Count
End Sub
or:
Code:
Sub ParagraphCount()
MsgBox ActiveDocument.ComputeStatistics(wdStatisticParagraphs)
End Sub
without:
• all the flickering;
• messing with the selected range;
• the overhead of a loop; and
• having to insert a quite arbitrary exit point to overcome your code's propensity for locking up Word?

Furthermore, your code doesn't handle tables correctly.

Moreover, since you haven't defined what 'counter' is, that:
• can result in complier errors (i.e. if one uses 'Option Explicit', which is always good practice); and
• results in an variant, which is less efficient to use than if it was properly defined it as a long.

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 02-21-2021, 07:58 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

Greg Maxey,
Yes, indeed, you may very well ask, "What is the point of that message box that tells you how many lines....."

I failed to mention, the msg box should be taken out, for actual use. But it sure was handy for developing and testing.

The point is not counting the lines, nor seeing the count.

The point is: Stopping the loop when the insertion point arrives on the last line of the document.

This Test returns = True - Only When the last Paragraph Mark in the document is highlighted.

I wrote this for myself to process a bulk of documents, assembled into one document, that were all similar except for a many variables like names and states; and where each document had to be re-arranged according to a certain formula. I needed some way to tell it to stop, end of document.
Reply With Quote
  #5  
Old 02-21-2021, 08:19 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

macropod,
Thank you for your reply.
I believe my reply to Greg Maxey will relieve some of your objections.

And I must confess to ignorance about your other objections.

But I am going to hang tough about the good utility of the Macro I wrote. It serves me very well.

And if I can help the next guy, then I am a happy man.
Reply With Quote
  #6  
Old 02-21-2021, 01:01 PM
macropod's Avatar
macropod macropod is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Stephen Ray View Post
I wrote this for myself to process a bulk of documents, assembled into one document, that were all similar except for a many variables like names and states; and where each document had to be re-arranged according to a certain formula. I needed some way to tell it to stop, end of document.
There are many code examples in this forum showing how to loop through paragraphs (if necessary) correctly. See, for example:
https://www.msofficeforums.com/123696-post2.html
https://www.msofficeforums.com/89357-post2.html
https://www.msofficeforums.com/word-...ge-header.html
https://www.msofficeforums.com/word-...nt-length.html
https://www.msofficeforums.com/139350-post2.html
https://www.msofficeforums.com/word-...ngle-line.html
None of these needs to select anything to do the required work and none needs an arbitrary counter to exit when done.

Content re-arrangement can often be done without the burden of looping through all paragraphs, though, via Find/Replace.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 02-21-2021, 05:26 PM
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

Macropod,
Thank you for your reply.


The purpose of my Macro is not to loop through paragraphs correctly.


The arbitrary counter is not needed to test:
Selection.Range.End = ActiveDocument.Range.End


However, I like the good counter for testing, development and demonstration purposes.


Content re-arrangement of sets of data does not lend itself to Find/Replace because each set has different variables.


My Macro solves the problem of working through the last set and stopping the loop after the last set.


It does this very well.
Reply With Quote
  #8  
Old 02-22-2021, 01:50 AM
macropod's Avatar
macropod macropod is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Since you haven't told us anything meaningful about what your macro is supposed to do, all we can draw conclusions on is what you have posted. Nothing you've told us so far supports your claim that your macro does anything very well.
Quote:
Originally Posted by Stephen Ray View Post
The arbitrary counter is not needed to test:
Selection.Range.End = ActiveDocument.Range.End


However, I like the good counter for testing, development and demonstration purposes.
With all due respect, that is not what your arbitrary counter counter does.

Quote:
Originally Posted by Stephen Ray View Post
Content re-arrangement of sets of data does not lend itself to Find/Replace because each set has different variables.
All that tells us is that you haven't learned to used Find/Replace to full advantage. I have posted many threads demonstrating how it can be used to re-arrange content containing variable data.
Quote:
Originally Posted by Stephen Ray View Post
It does this very well.
Quite the contrary, in fact.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-15-2023, 12:16 PM
Donfaison Donfaison is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Novice
 
Join Date: Feb 2023
Posts: 2
Donfaison is on a distinguished road
Default

"Do Until Selection.Range.End = ActiveDocument.Range.End" becomes an infinite loop for me. My code successfully finds wdstyleHeading5 (a title from a journal) and converts the 1st line below it to wdstyleHeading6, the collapses the abstract beneath. This allows me to quickly create an outline and show the first line of the citation (journal & date) when I print. However, once it gets to the end it simply keeps on searching. Any thoughts"
Sub Create_Collapsed_Heading6()
Selection.HomeKey Unit:=wdStory
Do Until Selection.Range.End = ActiveDocument.Range.End
Selection.Find.Style = ActiveDocument.Styles("Heading 5")
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Style = wdStyleHeading6
Selection.Paragraphs(1).CollapsedState = True
Loop
ActiveDocument.PrintOut
End Sub
Reply With Quote
  #10  
Old 02-15-2023, 07:01 PM
Guessed's Avatar
Guessed Guessed is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The Selection doesn't move if there is nothing else found so your Selection could only match the document end if the last paragraph was a Heading 5 - and then your 'next paragraph' wouldn't exist. Also, the find command has 'sticky' settings which could be affecting the search direction or other parameters. You do need to be explicit to ameliorate the impact of previous searches. eg
Code:
Sub Create_Collapsed_Heading6()
  Dim aRng As Range, aRngNext As Range
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Text = ""
    .Style = ActiveDocument.Styles(wdStyleHeading5)
    Do While .Execute = True
      Set aRngNext = aRng.Paragraphs(1).Next.Range
      aRngNext.Style = wdStyleHeading6
      aRngNext.Paragraphs(1).CollapsedState = True
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
  ActiveDocument.PrintOut
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 02-16-2023, 12:09 PM
Donfaison Donfaison is offline Loop Until End of Document Windows 10 Loop Until End of Document Office 2016
Novice
 
Join Date: Feb 2023
Posts: 2
Donfaison is on a distinguished road
Default

I'm not sure I understand everthing in that code, but it works like a charm! thanks!
Reply With Quote
Reply

Thread Tools
Display Modes


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 03:43 PM.


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