简体   繁体   English

带有字符缩进的 VB.NET MS-WORD 数字列表

[英]VB.NET MS-WORD Number List with Character Indents

So it has been a long time since I have attempted to do any word automation but long story short, I need to create a work document that includes a list.因此,自从我尝试进行任何单词自动化已经很长时间了,但长话短说,我需要创建一个包含列表的工作文档。 The first level being numbers and the second being letters, basically mimicking the out of the box default numbering format but can't quite get it figured out.第一级是数字,第二级是字母,基本上模仿了开箱即用的默认编号格式,但无法完全弄清楚。

The code I have so far just continues the numeric pattern on indent.到目前为止,我的代码只是继续缩进的数字模式。

Here is the code I have so far and a screen capture of what I need.这是我到目前为止的代码和我需要的屏幕截图。 Thanks谢谢

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

End Class结束类

What I need:我需要的: 在此处输入图片说明

So after a couple more hours of messing with this I found the solution.因此,经过几个小时的混乱之后,我找到了解决方案。

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

Main Method Apply the template: Main 方法 应用模板:

paragraph.Range.ListFormat.ApplyListTemplate(ParagraphTemplate)

Then we adding text levels you would use a call like this:然后我们添加文本级别,您将使用这样的调用:

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

Hope this helps someone else too!希望这对其他人也有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM