none
Apply Simple Numbering to Multiple Paragraph Styles? RRS feed

  • Question

  • I seem to be able to have new styles (created in VBA) inherit numbering if I place the "cursor" within an existing, numbered paragraph when the styles are created. However, missed numbering seems to occur, and I'm afraid this approach may not apply across Word 2010 and 2013.

    I'm starting with a document containing a group of numbered paragraphs created when the paragraphs were originally created using a leftover, (likely a recorded macro) from a previous version of Word:

    Sub ApplyNumbering()
           Selection.WholeStory
        
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
            .NumberFormat = "%1."
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleArabic
            .NumberPosition = InchesToPoints(0)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = 22
            .TabPosition = 22
            .ResetOnHigher = 0
            .StartAt = 1
            With .Font
                .Bold = False
                .Italic = False
                .StrikeThrough = wdUndefined
                .Subscript = wdUndefined
                .Superscript = wdUndefined
                .Shadow = wdUndefined
                .Outline = wdUndefined
                .Emboss = wdUndefined
                .Engrave = wdUndefined
                .AllCaps = wdUndefined
                .Hidden = wdUndefined
                .Underline = wdUndefined
                .Color = wdUndefined
                .Size = 11
                .Animation = wdUndefined
                .DoubleStrikeThrough = wdUndefined
                .Name = "Times New Roman"
            End With
            .LinkedStyle = ""
        End With
        ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""
        Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
            wdNumberGallery).ListTemplates(1), ContinuePreviousList:=False, ApplyTo:= _
            wdListApplyToWholeList ' W2013 removed, DefaultListBehavior:=wdWord10ListBehavior
    
        'W2013 fix list style defaulting to not showing space after paragraph
        ActiveDocument.Styles("List Paragraph").NoSpaceBetweenParagraphsOfSameStyle = False
    End Sub

    My project takes the previously-created document and creates 8 paragraph styles to be applied as required. Is there a way to take control of this numbering and have a simple, numbered list that applies across the 8, randomly-applied paragraph styles? By the way, there are intervening, un-numbered, paragraphs or page break paragraphs with no numbers.

    My searches seem to indicate the "norm" is multiple paragraph styles linked to a single, outline format, but I have not found any reference to a common numbering scheme applied across multiple paragraph styles.


    GrouseHiker

    Thursday, February 13, 2014 2:12 AM

All replies

  • Hi GrouseHiker

    Your exact requirement is not clear. Do you mean you want to link multiple paragraph styles to a numbering style? Or to apply numbering after-the-fact to various paragraphs (no matter the style used)?

    The former is NOT possible; the latter should be...

    Which version of Word? I see 2013 mentioned in comments in the code...

    The "tricky" thing with the code you show us is that it appears to be linked to a "Gallery" entry that the user sees in the UI. You'll notice that the ListTemplate is not given a name. The approach that will give the best control is to create a ListTemplate, assign it a name, and define the level you want to use. You can then use the Convert method on it to reduce it to a SingleLevel (by default, a ListTemplate is multi-level). You can then apply it to whatever paragraph(s) you wish. By default, the numbering should be continuous.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, February 13, 2014 2:58 PM
    Moderator
  • Thanks Cindy,

    I'm working in Word 2013, but my goal is to have the procedure apply to W2010 as well. I cleaned up that code a bit and it seems to be working when applied to a single paragraph. I don't really need the list style to show in the UI so here's what I did:

    Sub NewLT()
    Dim CapNumbering As ListTemplate
    Set CapNumbering = ActiveDocument.ListTemplates.Add
    
    With CapNumbering.ListLevels(1)
        .NumberFormat = "%1."
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = 0
        .Alignment = wdListLevelAlignLeft
        .TextPosition = 22
        .TabPosition = 22
        .ResetOnHigher = False
        .StartAt = 1
        .LinkedStyle = ""
    End With
    CapNumbering.Convert (1)
        Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=CapNumbering, ContinuePreviousList:=True, ApplyTo:= _
            wdListApplyToWholeList
    
    Running the macro manually, the numbering is not continuing in the following paragraphs. I changed ContinuePreviousList:=False to =True (as shown), but this didn't help. Am I doing something wrong?


    GrouseHiker


    edit: adding CapNumbering.Convert (1) (added to the code above) did not continue the numbering...
    • Edited by GrouseHiker Thursday, February 13, 2014 6:57 PM
    Thursday, February 13, 2014 6:25 PM
  • If you base a paragraph style on a numbered paragraph style, they will both share the same numbering scheme, if that helps. Note that you won't be able to customize any aspect of numbering in the "child" style (because it will break the numbering).


    Stefan Blom, Microsoft Word MVP

    Thursday, February 13, 2014 7:27 PM
  • Thanks Stefan,

    I thought I had tried that ... using in my style creation:

    .BaseStyle = "List Paragraph"

    I was assuming List Paragraph is a built-in List Style; however, when I go back and look, I see it's not a List Style.

    So, I'll try creating a list style from scratch and try your method as well as Cindy's.

    David Strickland


    GrouseHiker

    Thursday, February 13, 2014 8:02 PM
  • By default, recent versions of Word apply List Paragraph when you apply a number format, but numbering isn't "contained" within that style. You need an actual numbered paragraph style, such as the built-in List Number style.

    In the user interface, you create single-level numbering for a paragraph style via the Modify Style dialog box. To create a multilevel list where each level is assigned to a unique paragraph style, take a look at http://shaunakelly.com/word/numbering/numbering20072010.html.


    Stefan Blom, Microsoft Word MVP

    Thursday, February 13, 2014 8:16 PM
  • Stephan,

    I've searched this topic quite a bit (including threads with input from you and Shauna), and I can't find the VBA to create a numbered style. Any guidance here would be appreciated.

    david


    GrouseHiker

    Thursday, February 13, 2014 8:26 PM
  • An idea...

    In my code above, can I create a List Style by setting:

    .LinkedStyle = "MyListStyleName"

    ??


    GrouseHiker

    Thursday, February 13, 2014 8:43 PM
  • This seems to work. Is it supposed to?

    Sub NewLT()
    Dim CapNumbering As ListTemplate
    Set CapNumbering = ActiveDocument.ListTemplates.Add
    ActiveDocument.Styles("BaseNumStyle").Delete
    
    ActiveDocument.Styles.Add "BaseNumStyle"  'xxx, wdStyleTypeList
    With ActiveDocument.Styles("BaseNumStyle")
        .Font.Name = "Times New Roman"
        .Font.Size = 11
        .Font.NumberSpacing = wdNumberSpacingDefault
        .Font.NumberForm = wdNumberFormDefault
        .BaseStyle = ""
    End With
    
    With CapNumbering.ListLevels(1)
        .NumberFormat = "%1."
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = 0
        .Alignment = wdListLevelAlignLeft
        .TextPosition = 22
        .TabPosition = 22
        .ResetOnHigher = 0
        .StartAt = 1
        .LinkedStyle = "BaseNumStyle"
    End With
    
        Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=CapNumbering, ContinuePreviousList:=False, ApplyTo:= _
            wdListApplyToWholeList ' W2013 removed, DefaultListBehavior:=wdWord10ListBehavior
    
        ActiveDocument.Styles("List Paragraph").NoSpaceBetweenParagraphsOfSameStyle = False
    End Sub
    I then manually created a new style based on BaseNumStyle (I will eventually do this via code); the new style applied numbering. Does this actually provided total control of paragraph numbering and style, or am I missing something?


    GrouseHiker

    Thursday, February 13, 2014 10:54 PM
  • The LinkedStyle property assigns a *paragraph* style to a list level in a multilevel list. Similarly, the BaseStyle property is only supported for paragraph styles (and character styles, which aren't really relevant in this context anyway). I don't think you can make it work with other style types.

    What I'm suggesting is that you set up a numbered paragraph style and then, to have more paragraph styles be part of the same numbering scheme, base those other styles on your initial paragraph style.

    I recommend that you test this in the user interface first, to see if it does what you are trying to accomplish.


    Stefan Blom, Microsoft Word MVP

    Friday, February 14, 2014 12:17 AM
  • My approach is as you recommended. After running the code above, the BaseNumStyle shows up in the UI Style Gallery. When I apply BaseNumStyle to a "plain" paragraph based on Normal, numbering is applied by BaseNumStyle.

    When I create a new style (Style1) in the UI based on BaseNumStyle, Style1 also applies numbering to a "plain" paragraph.

    Applying Style1 to various paragraphs produces sequential numbering.  I'm just afraid my observations may not truly prove success.

    I will experiment more with this.


    GrouseHiker


    • Edited by GrouseHiker Friday, February 14, 2014 12:29 AM
    Friday, February 14, 2014 12:28 AM
  • I took this further and created (in VBA) "working" styles Test1 and Test2 based on BaseNumStyle. When Test1 and Test2 are randomly applied to paragraphs, the numbering is continuous. From the XML, it appears only BaseNumStyle is linked to numbering, with Test1 and Test2 inheriting numbering via the reference to BaseNumStyle:

    -<w:style w:styleId="Test1" w:type="paragraph" w:customStyle="1">
        <w:name w:val="Test1"/>
        <w:basedOn w:val="BaseNumStyle"/>
        <w:link w:val="Test1Char"/>
        <w:rsid w:val="007F089D"/>
        -<w:pPr>
            <w:keepLines/>
            <w:spacing w:lineRule="auto" w:line="240"/>
            <w:jc w:val="both"/>
        </w:pPr>
        -<w:rPr>
            <w:rFonts w:cs="Times New Roman"/>
        </w:rPr>
    </w:style>

    BaseNumStyle is linked to numbering via:

    w:numId w:val="2"/

    The numbering Xml includes an abstract numbering definition "0" linked to BaseNumStyle:

    -<w:abstractNum w:abstractNumId="0">
        <w:nsid w:val="1DB14FBE"/>
        <w:multiLevelType w:val="singleLevel"/>
        <w:tmpl w:val="13006700"/>
        -<w:lvl w:ilvl="0">
            <w:start w:val="1"/>
            <w:numFmt w:val="decimal"/>
            <w:lvlRestart w:val="0"/>
            <w:pStyle w:val="BaseNumStyle"/>
            <w:lvlText w:val="%1."/>
            <w:lvlJc w:val="left"/>
            -<w:pPr>
                -<w:tabs>
                    <w:tab w:val="num" w:pos="480"/>
                </w:tabs>
                <w:ind w:firstLine="0" w:left="0"/>
            </w:pPr>
        </w:lvl>
    </w:abstractNum>
    and the numbering definition instance "2" reference by the BaseNumStyle style definition:
    -<w:num w:numId="2">
        <w:abstractNumId w:val="0"/>
    </w:num>
    It appears my "working" styles, Test1 and Test2 are reliably linked to numbering via the base-style reference to BaseNumStyle.


    GrouseHiker


    • Edited by GrouseHiker Saturday, February 15, 2014 2:30 PM
    Saturday, February 15, 2014 2:28 PM
  • Eliminating the With group line:

      .LinkedStyle = "BaseNumStyle"

    and adding:

    ActiveDocument.Styles("BaseNumStyle").LinkToListTemplate ListTemplate:=CapNumbering

    also works.

    EDIT: Testing revealed the numbering was actually outline numbering. I changed this line:

    ActiveDocument.Styles("BaseNumStyle").LinkToListTemplate _
        ListTemplate:=CapNumbering, ListLevelNumber:=1

    and the outline numbering is not applied to the paragraph (i.e., tab key does not change the numbering). However, examining the Xml reveals the full complement of outline numbering definitions is created.

    I'm thinking at this point the .LinkedStyle Property is the way to go.



    GrouseHiker


    • Edited by GrouseHiker Saturday, February 15, 2014 11:08 PM
    Saturday, February 15, 2014 7:11 PM