none
Continue list numbering RRS feed

  • Question

  • Hi!

    This is for Word 2010, using C# from my own application.

    I have these 3 lists in the source document:

    1. First
    2. Second
    2.1 Subsection

    1. Second list, first
    2. Second list, second

    1. Third list, first
    2. Third list, second

    I want to have:

    1. First
    2. Second
    2.1 Subsection
    3. Second list, first
    4. Second list, second
    5. Third list, first
    6. Third list, second

    That is, i want to continue numbering after the first list. This is the code i have tried, which seems to be fine, but does not give me what i want:

    Word.Application wordApplication = new Word.Application();
    Word.Document wordDocument = wordApplication.Documents.Open(ref sourceFile, ReadOnly:true);
    wordDocument.Lists[2].ApplyListTemplateWithLevel(wordDocument.ListTemplates[1], ContinuePreviousList: true, DefaultListBehavior: Word.WdDefaultListBehavior.wdWord10ListBehavior);
    wordDocument.Lists[3].ApplyListTemplateWithLevel(wordDocument.ListTemplates[1], ContinuePreviousList: true, DefaultListBehavior: Word.WdDefaultListBehavior.wdWord10ListBehavior);
    wordDocument.SaveAs2(ref outputFile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    Instead, this is what i get:

    1.
    2.
    2.1
    1.
    3.
    1.
    4.

    So i end up with three lists. (two lists with only one list item, and one list with list item 1 - 4).

    What am i doing wrong? I just cannot figure it out.


    Regards, Kristofer Gafvert
    www.it-notebook.org

    Tuesday, September 17, 2013 10:45 AM

Answers

  • The problem here is that you can't easily tell if a list is a restarted list or a completely new list—unless you apply it with styles of course. The ApplyListTemplate method applies a list template to what Word considers to be a single list, not to what appears to be a list (from our perspective).

    Note that you can't record the procedure of creating a list style; instead, you will have to create the code manually from start to finish.


    Stefan Blom, Microsoft Word MVP

    Monday, September 23, 2013 8:39 AM

All replies

  • I also find this method does not work as we want.

    we can use record macro for word to get the code then find the same way in C#.

    After resetting number, I get VBA code like this:

    Sub Macro2()
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
            .NumberFormat = "%1."
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleArabic
            .NumberPosition = InchesToPoints(0.25)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(0.5)
            .TabPosition = wdUndefined
            .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 = ""
        End With
        With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(2)
            .NumberFormat = "%1.%2"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(0.5)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(0.76)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(3)
            .NumberFormat = "%1.%2.%3"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(0.75)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(1.25)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(4)
            .NumberFormat = "%1.%2.%3.%4"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(1)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(1.75)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(5)
            .NumberFormat = "%1.%2.%3.%4.%5"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(1.25)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(2)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(6)
            .NumberFormat = "%1.%2.%3.%4.%5.%6"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(1.5)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(2.5)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(7)
            .NumberFormat = "%1.%2.%3.%4.%5.%6.%7"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(1.75)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(2.75)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(8)
            .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(2)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(3.25)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).ListLevels(9)
            .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8.%9"
            .TrailingCharacter = wdTrailingTab
            .NumberStyle = wdListNumberStyleLegal
            .NumberPosition = InchesToPoints(2.25)
            .Alignment = wdListLevelAlignLeft
            .TextPosition = InchesToPoints(3.5)
            .TabPosition = wdUndefined
            .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(wdNumberGallery).ListTemplates(1).Name = ""
        Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
            ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
            True, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
            wdWord10ListBehavior
    End Sub


    Monday, September 23, 2013 3:25 AM
  • Magically the list numbers worked when merging two other documents, so i started to think why. These two documents were based on the same template, so the exact same list style was used for both lists.

    In a new document i created two lists, but with different list styles (created a new list style) and then tried to continue numbering in word for the second list. It did not work.

    So i suppose that "continue numbering" actually means to continue numbering from the previous list with the same list style.

    I would have thought that List.ApplyListTemplate actually would apply the list template i have chosen (in my example the first list), for all paragraphs in the new list. But it seems to apply the template to only paragraph two and later.


    Regards, Kristofer Gafvert
    www.it-notebook.org

    Monday, September 23, 2013 5:49 AM
  • The problem here is that you can't easily tell if a list is a restarted list or a completely new list—unless you apply it with styles of course. The ApplyListTemplate method applies a list template to what Word considers to be a single list, not to what appears to be a list (from our perspective).

    Note that you can't record the procedure of creating a list style; instead, you will have to create the code manually from start to finish.


    Stefan Blom, Microsoft Word MVP

    Monday, September 23, 2013 8:39 AM
  • I am not really sure that i understand everything about how this works. But i think i understand enough to get my task done. Otherwise i will come back.  :-)

    Thanks!


    Regards, Kristofer Gafvert
    www.it-notebook.org

    Monday, September 23, 2013 1:16 PM