Link to home
Start Free TrialLog in
Avatar of Joshua Martinez
Joshua Martinez

asked on

Creating a "Do Loop" that ends at the last "Heading 2" style of a document

I'm working on a macro that inserts a style separator before the first period in every heading level 2 paragraph. It is also set up to skip any paragraph that is not heading 2. Currently it only works as many times as you specifically trigger it, but I would like it to continue looping until the last style separator is inserted in the last heading 2 paragraph. I can't figure out how to loop this without creating an infinite loop. I'm new to the macro writing game so please excuse my code:

      
 If Selection.Style = "Heading 2" Then
            Selection.MoveUntil Cset:=".", Count:=100
            Application.Run MacroName:="LWmacros.basHNum.InsertStyleSeparator"
            ActiveWindow.Selection.GoTo wdGoToHeading, wdGoToNext

ElseIf Selection.Style <> "Heading 2" Then
             ActiveWindow.Selection.GoTo wdGoToHeading, wdGoToNext

End If 

Open in new window



 I'm not sure what sort of condition I would need to set for the Do Loop for it to end after the last heading level 2 paragraph is found.
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Please post your InsertStyleSeparator macro code.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that was not your intention, perhaps you should explain what you are trying to do with the macro, regardless of how to code it.
Avatar of Joshua Martinez
Joshua Martinez

ASKER

This worked perfectly! All I needed to do was replace the "selection.InsertStyleSeparator" with our in-house style separator macro we created. You are a life saver, truly.
I couldn't be any happier with this solution.