Solved

VBA code to change numbering styles in existing Multilevel list

Posted on 2010-08-30
7
4,092 Views
Last Modified: 2012-05-10
Hi experts

I have a template in which I have set up several Multilevel lists linked to styles. My client now wants to be able to change the setup of one of these lists by running a macro. So for instance instead of
1.
1.1
(a)

Change this to
1.
(a)
(i)

This concept anyway.

In this list I have given a name of "ComplexNo2" to the ListNum field list_name.

I recorded my key strokes to change this list, but I'm concerned about running the code - how will it know what list to pick up and adjust? For instance the first line concerns me.
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
Right at the end there is
ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = "ComplexNo2"
but I'm not sure how this comes into affect.

Is there a way to tell it which list, or should the recorded macro be fine to use?

Also, if anyone knows a good article to read on working with multilists with vba would appreciate the referral.


Sub RecordedMacro()
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleNone
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1)
        .TabPosition = CentimetersToPoints(1)
        .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 = "Complex Numbering Level 1"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2)
        .NumberFormat = "%2%1."
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1)
        .TabPosition = wdUndefined
        .ResetOnHigher = 1
        .StartAt = 1
        With .Font
            .Bold = False
            .Italic = False
            .StrikeThrough = False
            .Subscript = False
            .Superscript = False
            .Shadow = False
            .Outline = False
            .Emboss = False
            .Engrave = False
            .AllCaps = False
            .Hidden = False
            .Underline = wdUnderlineNone
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = False
            .Name = "Times New Roman"
        End With
        .LinkedStyle = "Complex Numbering Level 2"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(3)
        .NumberFormat = "(%3)"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleLowercaseLetter
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1.8)
        .TabPosition = CentimetersToPoints(0.8)
        .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 = wdColorAutomatic
            .Size = 8
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = "Franklin Gothic Book"
        End With
        .LinkedStyle = "Complex Numbering Level 3"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(4)
        .NumberFormat = "(%4)"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleLowercaseRoman
        .NumberPosition = CentimetersToPoints(1.6)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(2.2)
        .TabPosition = CentimetersToPoints(2.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 = "Complex Numbering Level 4"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(5)
        .NumberFormat = "(%5)"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleLowercaseLetter
        .NumberPosition = CentimetersToPoints(2.2)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(2.8)
        .TabPosition = CentimetersToPoints(2.8)
        .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 = "Complex Numbering Level 5"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(6)
        .NumberFormat = ""
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleNone
        .NumberPosition = CentimetersToPoints(2.8)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(4.42)
        .TabPosition = CentimetersToPoints(4.68)
        .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(1).ListLevels(7)
        .NumberFormat = ""
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleNone
        .NumberPosition = CentimetersToPoints(2.8)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(5.31)
        .TabPosition = CentimetersToPoints(5.95)
        .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(1).ListLevels(8)
        .NumberFormat = ""
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleNone
        .NumberPosition = CentimetersToPoints(2.8)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(6.2)
        .TabPosition = CentimetersToPoints(6.58)
        .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(1).ListLevels(9)
        .NumberFormat = ""
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleNone
        .NumberPosition = CentimetersToPoints(2.8)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(7.22)
        .TabPosition = CentimetersToPoints(28)
        .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(1).Name = "ComplexNo2"
    Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
        ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
        DefaultListBehavior:=wdWord10ListBehavior
End Sub

Open in new window

0
Comment
Question by:Fi69
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:lwebber
ID: 33577612
Tell me more about the styles. Let's just deal with your first numbering scheme for now (1., 1.1, (a)).

Two possibilities:

A. You want to use a single style name, like

    FooStyle

When you apply that style to a paragraph, it automatically gets the number 1. If you hit enter to get a new paragraph, that one gets the number 2. If you then hit  the Increase Indent button on the toolbar, it gets the number 1.1 (because the previous para is 1.), BUT the style name stays the same (FooStyle).

B. You have a set of styles called

    BarStyle 1
    BarStyle 2
    ...
    BarStyle 9

When you apply the BarStyle 1 to a paragraph, it automaticallly gets the number 1. If you hit enter to get a new paragraph, that one gets the number 2. If you then hit  the Increase Indent button on the toolbar, it gets the number 1.1 (because the previous para is 1.), AND ITS STYLE NAME AUTOMATICALLY changes to BarStyle 2.

Scenario B is similar to the built-in styles called Heading 1, Heading 2, etc.

Your choice of A or B will determine the answer to your question.
0
 

Author Comment

by:Fi69
ID: 33583555
HI lwebber - the answer is B
0
 
LVL 9

Expert Comment

by:lwebber
ID: 33585248
Would it be OK to use the built-in Heading 1, Heading 2... styles? If not, what style name(s) are you using?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Accepted Solution

by:
Fi69 earned 0 total points
ID: 33585433
Hi lwebber

After extensive search on the internet I finally stumbled across a solution today.  Here's the link:
http://www.eggheadcafe.com/aspnet_answers/wordnumbering/Aug2006/post27666622.asp
I've posted the code below too for convenience.

It is so hard to find anything on this subject.

Just a note on the code below - It is important that the multilevel list is named as I had done, ie "ComplexNo2".

Sub CreateListNumbering() 
 
Dim LT As ListTemplate 
 
Dim mylist As ListTemplate 
 
For Each LT In ActiveDocument.ListTemplates 
 
If LT.Name = "my list" Then 
 
Set mylist = LT 
 
Exit For 
 
End If 
 
Next LT 
 
If mylist Is Nothing Then 
 
Set mylist = ActiveDocument.ListTemplates.Add _ 
 
(OutlineNumbered:=True, Name:="my list") 
 
End If 
 
With mylist 
 
With .ListLevels(1) 
 
.NumberFormat = "%1." 
 
.TrailingCharacter = wdTrailingTab 
 
.NumberStyle = wdListNumberStyleArabic 
 
.NumberPosition = InchesToPoints(1) 
 
.Alignment = wdListLevelAlignLeft 
 
.TextPosition = InchesToPoints(1.25) 
 
.TabPosition = InchesToPoints(1.25) 
 
.ResetOnHigher = 0 
 
.StartAt = 1 
 
'Set a linked style: 
 
.LinkedStyle = "Heading 1" 
 
End With 
 
'Repeat With .ListLevels(n) 
 
'for n=2,...,9 
 
'and set the desired options 
 
'for each level 
 
End With 
 
End Sub 
 
Then use styles to apply numbering in the document. 
 
-- 
 
Stefan Blom 
 
Microsoft Word MVP 
 

Open in new window

0
 

Author Comment

by:Fi69
ID: 33585438
By the way thanks for trying to help me!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33588000
Here is an article which explains List, and other,  numbering


http://word.mvps.org/faqs/numbering/WordsNumberingExplained.htm
0
 

Author Comment

by:Fi69
ID: 33592599
Thanks Graham

Shall have a read of this.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert .PDF 6 66
Excel copy picture into Outlook email 7 55
Microsoft Word startup Issue 7 25
Word and Excel 2013 - Disabled Items List 3 62
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question