2017-01-23 63 views
0

因此,自從我試圖做任何單詞自動化但長話短說以來,我已經有很長時間了,我需要創建一個包含列表的工作文檔。第一級是數字,第二級是字母,基本上模仿了開箱即用的默認編號格式,但無法完全弄清楚。帶有字符縮進的VB.NET MS-WORD數字列表

我到目前爲止的代碼只是在indent上繼續數字模式。

這是我到目前爲止的代碼和我需要的屏幕截圖。由於

Public NotInheritable Class Utilities 
Public Shared Sub CreateDocument() 
    'Local Variable Declaration 
    Dim application As New Microsoft.Office.Interop.Word.Application 
    Dim document As Microsoft.Office.Interop.Word.Document 
    Dim range As Microsoft.Office.Interop.Word.Range 

    application.Visible = True 

    'Add a new document 
    document = application.Documents.Add() 

    'Add Header and Footer 
    For Each Item As Microsoft.Office.Interop.Word.Section In document.Sections 
     'Header 
     Dim header As Microsoft.Office.Interop.Word.Range = Item.Headers(Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range 
     header.Fields.Add(header, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage) 
     header.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter 
     header.Text = "Header" 
     header.Font.Name = "Arial" 
     header.Font.Size = 10.0 
     header.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed 

     'Footer 
     Dim footer As Microsoft.Office.Interop.Word.Range = Item.Footers(Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range 
     footer.Fields.Add(footer, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage) 
     footer.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter 
     footer.Text = "Footer" 
     footer.Font.Name = "Arial" 
     footer.Font.Size = 10.0 
     footer.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorRed 
    Next 

    'Setup Default Range 
    range = document.Range() 
    range.Style = document.Styles("No Spacing") 
    range.Font.Name = "Arial" 
    range.Font.Size = 10.0 
    range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack 

    range.Text = "Line 1" & vbCrLf 
    range.Text &= "Line 2" & vbCrLf & vbCrLf 

    Dim paragraph As Microsoft.Office.Interop.Word.Paragraph = range.Paragraphs.Add 
    paragraph.Range.Text = "First Numbered Line:" 
    paragraph.Range.ListFormat.ApplyNumberDefault(Microsoft.Office.Interop.Word.WdDefaultListBehavior.wdWord10ListBehavior) 
    paragraph.Outdent() 

    Dim list = paragraph.Range.ListFormat.ListTemplate.ListLevels(1).NumberStyle = Microsoft.Office.Interop.Word.WdListNumberStyle.wdListNumberStyleLowercaseLetter 

    paragraph.Range.Paragraphs.Add() 
    paragraph.Range.Paragraphs(1).Range.Text = "Second Character Line" 
    paragraph.Range.Paragraphs(1).Range.ListFormat.ApplyListTemplate(document.ListTemplates(1), True, list) 
    paragraph.Range.Paragraphs(1).Indent() 

    paragraph.Range.InsertParagraphAfter() 
End Sub 

末級

我需要什麼: enter image description here

+0

更容易在Word中記錄宏,並檢查生成的代碼(這就是我會做的,無論如何給你答案)。它是列表屬性之一,但現在不記得它。 – Slai

回答

0

所以一對夫婦更小時,這種搞亂後,我找到了解決辦法。

Private Shared Sub SetupParagraphsTemplates(Application As Microsoft.Office.Interop.Word.Application) 
    ParagraphTemplate = Application.ListGalleries(Microsoft.Office.Interop.Word.WdListGalleryType.wdOutlineNumberGallery).ListTemplates(2) 
    ParagraphTemplate.ListLevels(1).NumberStyle = Microsoft.Office.Interop.Word.WdListNumberStyle.wdListNumberStyleArabic 
    ParagraphTemplate.ListLevels(2).NumberFormat = "%2." 
    ParagraphTemplate.ListLevels(2).NumberStyle = Microsoft.Office.Interop.Word.WdListNumberStyle.wdListNumberStyleLowercaseLetter 
    ParagraphTemplate.ListLevels(3).NumberStyle = Microsoft.Office.Interop.Word.WdListNumberStyle.wdListNumberStyleArabic 
    ParagraphTemplate.ListLevels(3).NumberFormat = "%3." 
End Sub 

主要方法應用模板:

paragraph.Range.ListFormat.ApplyListTemplate(ParagraphTemplate) 

然後我們添加文字水平,你會使用一個電話這樣的:

‘Text 
    range.Paragraphs.Add() 
    range.Paragraphs(range.Paragraphs.Count).Range.Text = "Test" 
    range.Paragraphs(range.Paragraphs.Count).Range.SetListLevel(2) 

希望這有助於別人呢!

相關問題