Change Word Outline Numbering via VBA (Word 2010)

I autoformat tables based on options that the user selects in a UserForm that pops up when the user runs the macro.  Some rows in the table are are just text, and some rows are outline numbered headings.  Sometimes the headings have manual numbering, and sometimes they don't.  One of the options I would like to provide to the users is to turn on or off the outline autonumbering in the Word document if the heading rows have manual numbering. 

It used to be easy to do before Word 2010 - I could turn Outline Numbering on or off for all the heading styles just by changing Heading 1 because all my other heading styles are based on Heading 1.  But with Word 2010, which has the new List Style Gallery for Outline Numbering, I am having a heck of a time figuring out how to apply (or remove) outline numbering from all 9 heading styles using VBA, and there doesn't seem to be much discussion about how to do this anywhere.  I have a named multilevel numbered list style in the gallery called "Headings" (as suggested by Shauna Kelly in her very informative tutorial about setting up list styles), but as far as I can figure out there isn't a way to apply a named list style to the selection using VBA.  Even if I could, I don't think the change would propagate to the remaining heading styles so that all the headings in the document inherit the numbering scheme. 

Anybody have any ideas?

Thanks

Susan

Answer
Answer

While Charles' method held some promise, I wanted to thoroughly explore the List Gallery idea before giving it up all together.  I spoke to a collegue of mine who is better versed in VBA than I am (he does Excel macros more than Word macros, but really knows his wa around VBA).  Anyway, we came up with this code, which works perfectly.

Dim myLT as ListTemplate

If DoAutoNum = True Then
    Set myLT = Application.ListGalleries.Item(wdOutlineNumberGallery).ListTemplates(5)
    With Selection.Find
        .Style = ActiveDocument.Styles("Heading 1")
        .Execute
    End With
    Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=myLT
End If

DoAutoNum is a variable that gets set to true if the user checks a box on the user form indicating that he wants to autonumber the document.  ListTemplates(5) is the built-in list template that has the outline numbering I wanted to use.  I took a SWAG about the number (it was the 5th one in the list), but it seems to be the correct number.   Fortunately I didn't need any kind of fancy numbering where I would need a custom outline numbered list style, because I never did figure out how to apply a named list style.

By default, the heading styles in my template are not autonumbered.  Since all my heading styles are based on Heading 1, applying the list template to the Heading 1 Style cascades to all my other styles too, which is exactly how I wanted it to work.  So, if the headings in the table have manual numbers before formatting, DoAutoNum is set to False, and this code never runs.  If the headings have no numbers, the user sets DoAutoNum to true and this code runs and autonumbers everything.  Works great.

Susan

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated November 12, 2022 Views 2,993 Applies to: