0
votes

I'm trying to make a VBA macro to create a word document from excel... Currently I'm having issues with setting up the Lists properly. I want the list indexes linked to my 2 header types. Like so:

1. Header1
 1.1. Header2
2. Header1
 2.1 Header2

The problem is my level 2 list is not reseting even though I've included the .ResetOnHigher property. This means I'm getting a result which looks like this:

1. Header1
 1.1. Header2
2. Header1
 1.2 Header2

Can someone tell me what I'm doing wrong and what can I do to resolve this issue?

Here is part of the code i'm using:

(...)

With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
   .NumberFormat = "%1."
   .TrailingCharacter = wdTrailingTab
   .NumberStyle = wdListNumberStyleArabic
   .NumberPosition = CentimetersToPoints(0)
   .Alignment = wdListLevelAlignLeft
   .TextPosition = CentimetersToPoints(0.63)
   .TabPosition = wdUndefined
   .StartAt = 1
End With

With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2)
   .NumberFormat = "%1.%2."
   .TrailingCharacter = wdTrailingTab
   .NumberStyle = wdListNumberStyleArabic
   .NumberPosition = CentimetersToPoints(0.63)
   .Alignment = wdListLevelAlignLeft
   .TextPosition = CentimetersToPoints(1.4)
   .TabPosition = wdUndefined
   .ResetOnHigher = 1
   .StartAt = 1
End With

With myDoc
'Heading 1
    .Styles(wdStyleHeading1).Font.Name = "Arial"
    .Styles(wdStyleHeading1).Font.Size = 24
    .Styles(wdStyleHeading1).Font.Color = wdColorBlack
    .Styles(wdStyleHeading1).Font.Bold = True
    .Styles(wdStyleHeading1).ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
    .Styles(wdStyleHeading1).ParagraphFormat.SpaceAfter = 12
    .Styles(wdStyleHeading1).LinkToListTemplate _
        ListTemplate:=ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
        ListLevelNumber:=1

'Heading 2
    .Styles(wdStyleHeading2).Font.Name = "Arial"
    .Styles(wdStyleHeading2).Font.Size = 18
    .Styles(wdStyleHeading2).Font.Color = wdColorBlack
    .Styles(wdStyleHeading2).Font.Bold = True
    .Styles(wdStyleHeading2).ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
    .Styles(wdStyleHeading2).ParagraphFormat.SpaceAfter = 12
    .Styles(wdStyleHeading2).LinkToListTemplate _
        ListTemplate:=ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
        ListLevelNumber:=2
End With

(...)

'Loop through sheets
For I = 2 To WS_Count - 1

'Check if sheet is to be included and if so past its content to word
If ThisWorkbook.Worksheets(I).Shapes("Enable").OLEFormat.Object.Value = 1 = True Then

'Insert Group Title if Group is different
 If ThisWorkbook.Worksheets(I).Cells(1, 1).Value = ThisWorkbook.Worksheets(I - 1).Cells(1, 1).Value = False Then

    myDoc.Paragraphs.Last.Range.Style = myDoc.Styles("Heading 1")
    myDoc.Paragraphs.Last.Range.Text = ThisWorkbook.Worksheets(I).Range("A1")
    myDoc.Paragraphs.Last.Range.InsertParagraphAfter

End If

'Insert Page Title
myDoc.Paragraphs.Last.Range.Style = myDoc.Styles("Heading 2")
myDoc.Paragraphs.Last.Range.Text = ThisWorkbook.Worksheets(I).Range("A2")
myDoc.Paragraphs.Last.Range.InsertParagraphAfter

'Insert Tables
Call ExcelRangeToWord(myDoc, ThisWorkbook.Worksheets(I).Range("range1"), 1)
myDoc.Paragraphs.Last.Range.InsertParagraph
Call ExcelRangeToWord(myDoc, ThisWorkbook.Worksheets(I).Range("range2"), 2)
myDoc.Paragraphs.Last.Range.InsertParagraph

'Insert Page Break on last paragraph
myDoc.Paragraphs.Last.Range.InsertBreak (wdPageBreak)

End If

(...)
1

1 Answers

0
votes

Well i had the exact same Problem and it took me a whole day to solve it. The Thing is that you are creating the Styles and within each style you are pointing on the List+Listlevel. When you are using These Styles each Style is creating ist own List. So The Solution is to do it the other way round.

You have to create the new List template with all ist listlevels and within each Listlevel you have to Point on the Styles like:

ActiveDocument.ListTemplates("LT").ListLevels(1).LinkedStyle = "Style1"

And when you want to use this specific Format you insert it via:

.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=ActiveDocument.ListTemplates("LT"), ContinuePreviousList:=True, ApplyLevel:=1

Have Fun =)