Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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: 4,176
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
 

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 08:30 AM.


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