VBA / Word添加项目符号,编号等

时间:2014-05-04 13:16:53

标签: vba ms-word word-vba

我一直在玩VBA代码,允许自动创建word文档。在下面的示例中,我写了一个Word段落6次。为了进行某些格式化(项目符号,编号,在表格中放置文本等等),似乎需要进行第二次传递并在创建文本后应用格式。这可以在一次通过中完成,如VBA允许我们使用粗体或斜体吗?

实施例: - 切换编号 - 写行

Sub Sample()
    Dim oWordApp As Object, oWordDoc As Object
    Dim i As Integer


    '~~> Establish an Word application object
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Set oWordApp = CreateObject("Word.Application")
    End If
    Err.Clear
    On Error GoTo 0

    oWordApp.Visible = True

    Set oWordDoc = oWordApp.Documents.Add

    With oWordDoc
        For i = 0 To 5
            .Content.InsertAfter ("Paragraph " & i)
            .Content.InsertParagraphAfter
        Next

        ' Yields execution so that the operating system can process other events.
        DoEvents

        ' why does this have to be done after creating text? 
        .Paragraphs(1).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

        .Paragraphs(2).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

    End With

    Set oWordApp = Nothing
    Set oWordDoc = Nothing
End Sub

来自Excel VBA for creating numbered list in Word

的示例

3 个答案:

答案 0 :(得分:2)

你显然可以这样做。几乎没有可能,但我相信下面的解决方案可以为您提供如何使用Document.Range(start,end)属性的线索。

 With oWordDoc
        For i = 0 To 5
            .Content.InsertAfter ("Paragraph " & i)
            .Content.InsertParagraphAfter
        Next

        ' Yields execution so that the operating system can process other events.
        DoEvents

 'apply numbering for 6 added paragraphs
   .Range(.Paragraphs(1).Range.Start, _
                .Paragraphs(6).Range.End) _
        .ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

'... the rest of your code here

答案 1 :(得分:2)

我在添加项目符号时遇到了一些问题,所以这里的代码对我有用。

请注意,这需要添加对Microsoft Word 15.0 Object Library

的引用
Sub Generate()
Dim objWord
Dim objDoc
Dim temp3 As Word.ListTemplate
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection

'Change your text here:
objSelection.TypeText ("This is my text in Word Document using Excel")
objSelection.TypeParagraph
objSelection.TypeText ("Here is more text")
'--------------------------

Set temp3 = objWord.ListGalleries(wdNumberGallery).ListTemplates(1)
With temp3.ListLevels(1)
    .Font.Name = "Symbol"
    .Font.Size = 11
    .NumberFormat = ChrW(61623)
    .TrailingCharacter = wdTrailingTab
    .NumberStyle = wdListNumberStyleArabic
    .NumberPosition = objWord.CentimetersToPoints(0.63)
    .Alignment = wdListLevelAlignLeft
    .TextPosition = objWord.CentimetersToPoints(1.27)
    .TabPosition = wdUndefined
    .StartAt = 1
End With

'Apply formatting to our range
objDoc.Range.ListFormat.ApplyListTemplate ListTemplate:=temp3
End Sub

答案 2 :(得分:0)

这样做......

    Dim wdApp As Word.Application
    Set wdApp = New Word.Application


    With wdApp
        .Visible = True
        .Activate
        .Documents.Add
        ' turn on bullets
        .ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
        .Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=.ListGalleries(wdBulletGallery).ListTemplates(1), _
            continuepreviouslist:=False, applyto:=wdListApplyToWholeList, defaultlistbehavior:=wdWord9ListBehavior

        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .Font.Bold = False
            .Font.Name = "Century Gothic"
            .Font.Size = 12
            .TypeText ("some details")
            .TypeParagraph
            .TypeText ("some details")
            .TypeParagraph
        End With

        ' turn off bullets
        .Selection.Range.ListFormat.RemoveNumbers wdBulletGallery

        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .TypeText ("some details")
            .TypeParagraph
            .TypeText ("some details")
            .TypeParagraph

        End With

        ' turn on outline numbers
        .ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = ""
        .Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=.ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
            continuepreviouslist:=False, applyto:=wdListApplyToWholeList, defaultlistbehavior:=wdWord9ListBehavior

        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .TypeText ("some details")
            .TypeParagraph
            .TypeText ("some details")

        End With

    End With