2014-10-08 78 views
0

我試圖讓一個VBA宏從Excel創建一個Word文檔... 目前我遇到了正確設置列表的問題。我想鏈接到我的2個頭類型的列表索引。 像這樣:重置帶有文本樣式的編號列表VBA宏MS Word

1. Header1 
1.1. Header2 
2. Header1 
2.1 Header2 

的問題是,即使我已經包括了.ResetOnHigher物業我的2級列表不正在重置。這意味着我得到的結果看起來像這樣:

1. Header1 
1.1. Header2 
2. Header1 
1.2 Header2 

有人能告訴我什麼,我做錯了,我能做些什麼來解決這個問題呢?

這裏是我使用的代碼的一部分:

(...) 

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 

(...) 

回答

0

嗯,我有完全相同的問題和我花了一整天的時間解決這個問題。事情是,你正在創建樣式,並在你指向List + Listlevel的每種樣式中。當您使用這些樣式時,每個樣式都會創建自己的列表。所以解決方法是反過來做。

您必須創建新的列表模板,所有北京時間listlevels每個Listlevel內你得點像的樣式:

ActiveDocument.ListTemplates(「LT」)ListLevels(1).LinkedStyle = 「樣式1」

而當你想用這種特定的格式,你通過插入:

.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= ActiveDocumen t.ListTemplates( 「LT」),ContinuePreviousList:= TRUE,ApplyLevel:= 1

有樂趣=)