how do I loop this macro?

T

Tom

I have a macro that I use to restart lists in a document. I created
this macro by recording a macro, so it's long and bulky. What it does
it search for the next occurence of the word "restartlist" and then it
manually restarts the numbering there.

It works, but I have to run the macro about a dozen or more times
depending on the number of lists I have in my document. I would like to
reduce this operation to one-click.

Is there any way to tell the macro to loop until it cycles through the
entire document?

Here is the macro code:

Sub restartlist()
'
' restartlist Macro
' Macro recorded 12/14/2006 by Tom Johnson
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "restartlist"
.Replacement.Text = "see Figure "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(1)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1.25)
.TabPosition = InchesToPoints(0)
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = "ListNumbered"
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(2)
.NumberFormat = "%2."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(1.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1.5)
.TabPosition = InchesToPoints(0)
.ResetOnHigher = 1
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(3)
.NumberFormat = "%3."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseRoman
.NumberPosition = InchesToPoints(1.75)
.Alignment = wdListLevelAlignRight
.TextPosition = InchesToPoints(2)
.TabPosition = InchesToPoints(0)
.ResetOnHigher = 2
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(4)
.NumberFormat = "%4."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(1.75)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(2)
.TabPosition = InchesToPoints(2)
.ResetOnHigher = 3
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(5)
.NumberFormat = "%5."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(2.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(2.5)
.TabPosition = InchesToPoints(2.5)
.ResetOnHigher = 4
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(6)
.NumberFormat = "%6."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseRoman
.NumberPosition = InchesToPoints(2.88)
.Alignment = wdListLevelAlignRight
.TextPosition = InchesToPoints(3)
.TabPosition = InchesToPoints(3)
.ResetOnHigher = 5
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(7)
.NumberFormat = "%7."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(3.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(3.5)
.TabPosition = InchesToPoints(3.5)
.ResetOnHigher = 6
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(8)
.NumberFormat = "%8."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(3.75)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(4)
.TabPosition = InchesToPoints(4)
.ResetOnHigher = 7
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(9)
.NumberFormat = "%9."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseRoman
.NumberPosition = InchesToPoints(4.38)
.Alignment = wdListLevelAlignRight
.TextPosition = InchesToPoints(4.5)
.TabPosition = InchesToPoints(4.5)
.ResetOnHigher = 8
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
ListGalleries(wdOutlineNumberGallery).ListTemplates(5).Name = ""
Selection.Range.ListFormat.ApplyListTemplate
ListTemplate:=ListGalleries( _
wdOutlineNumberGallery).ListTemplates(5),
ContinuePreviousList:=False, _
ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
End Sub
 
S

Shauna Kelly

Hi Tom

Writing code to do with lists is not for the feint-hearted.

The big issue with your code is that it uses the ListGalleries, which is
VBA's way of referring to the 8 boxes you see in the Bullets and Numbering
dialog box. Since what's in the ListGalleries potentially changes each time
you use them, then your code won't be stable. What it does today, it may not
do tomorrow, and it certainly won't do it on someone else's machine.

Instead of using the ListGalleries, you have to get the ListTemplate
attached to the relevant first-level style, and manipulate that.

I can't tell enough from the code what it is exactly you're trying to do.
But for more general help about these issues, I suggest you start with
these:

How to restart style-based numbering
http://www.word.mvps.org/FAQs/Numbering/ListRestartMethods.htm

How to modify a recorded macro
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm

and finally

Word's numbering explained
http://www.word.mvps.org/FAQs/Numbering/WordsNumberingExplained.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
T

Tom

Shauna,

I just tried it on another person's computer and it worked. Basically
our situation is a little bit different. We have ten tech writers who
all use the same document template. The macro is associated with the
list level styles in the template, so that avoids the problem of
different list styles on different computers.

The macro basically works like this: Find the next instance of the
RestartList style. Then when that next instance is located, manually
restart the list by going into bullets and numbering and checking the
restart box. (At least I think that's what I did instead of clicking
the Restart Numbering button -- but I can't remember right now).

Then after I'm done restarting the lists, I swap the Restart List style
to be a hidden style.

I really did read most of the tutorials on your site while figuring
this out. They were pretty helpful. But I think in my situation, I'm
dealing with documents generated from RoboHelp, not documents whose
source material is authored in Word, so I couldn't apply some of the
techniques. For example, I tried restarting lists by beginning each
first item with a new style. But that didn't seem to have an effect. (I
guess the RestartList style is essentially the same thing, though!)

Still, my method requires that I click the restart button numerous
times. Is there a way to have a macro loop so that it goes until it
finds all instances of the RestartList style? I had to do it one by one
because when I did it all at the same time, Word broke.

Thanks for your help.

Tom
 

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