Please help with looping in macro

J

Jason Logue

Hi, I posted this to the vba programming list, but I thought I should
post it to the vba for beginners site as well since I qualify myself
as a beginner. I am working on a macro that finds each instance of a
Heading 6, then
moves one line below that text to the formatted numbered list and sets
the
numbered list to start at one. I am still new at this, so I am trying
to
keep it simple (i.e. I am not sure how to tell it to look only for
lists that
begin with 2,3,4,5, etc, but not 1). I want the code to repeat until
all the
numbered lists following heading sixes are restarted. Here is the
code I
have thus far (as you can probably tell, it's not working):


Const LookFor As String = "Recommendations:"
'
With ActiveDocument.Content.Find
.ClearFormatting
.Text = LookFor
With .Style = ActiveDocument.Styles("Heading 6")
End With

Do While .Execute
Selection.MoveDown Unit:=wdLine, Count:=1
With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0)
.TabPosition = InchesToPoints(0#)
.ResetOnHigher = 0
.StartAt = 1
.LinkedStyle = "List Number"
End With

Loop

TIA - Jason
 
K

Klaus Linke

Hi Jason,

I love
Const LookFor As String = "Recommendations:"

Fitting!

Selection.Find is simpler than using a Range (which would require you to
define one in the first place), and since you want to search for some
formatting ("Heading 6"), you need to set ".Format = True".

The "With/End With" in
With .Style = ActiveDocument.Styles("Heading 6")
End With
is redundant (... it doesn't hurt, but can be removed).

OTOH, an "End With" is missing at the very end of your macro.

The list galleries are best avoided. You can access the list template that
is responsible for the current numbering directly.

You'd end up with some macro like the one below.

You also might check out whether you can incorporate "Heading 6" and the
list style you are using for the following paragraph in the same list
template.
But if you only want to restart if "Heading 6" contains the string
"Recommendations:", you need the macro... or a separate "Recommendations"
style that looks like a "Heading 6".

Regards,
Klaus


Const LookFor As String = "Recommendations:"
'
Selection.End = 0
Selection.Find.ClearFormatting
Selection.Find.style = ActiveDocument.Styles("Heading 6")
With Selection.Find
.Text = LookFor
.Wrap = wdFindStop
.Format = True
Do While .Execute
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Range.ListFormat
.ApplyListTemplate _
ListTemplate:=.ListTemplate, _
ContinuePreviousList:=False, _
ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord9ListBehavior
End With
Loop
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top