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

我一直在使用允许自动创建Word文档的VBA代码。 在下面的示例中,我编写了一个Word段落6次。 为了进行某些格式设置(项目符号,编号,将文本放入表格等),在创建文本之后似乎需要进行第二遍处理并应用格式设置。 可以像VBA这样允许我们用粗体或斜体来完成一次操作吗?

例:
-切换编号
-写线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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中用于在Word中创建编号列表的示例

 


 

我在添加项目符号时遇到了一些麻烦,因此这是对我有用的代码。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

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

 


 

您显然可以做到。 可能性很小,但我相信以下解决方案为您提供了有关如何使用Document.Range(start,end)属性的线索。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

 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)将样式设置为"标题1" 2)写标题3)新段落4)将段落设置为项目符号列表(或编号列表)5)写段落.. 重复6)写n个段落...然后重复
  • 我对编写段落不满意(然后在后面添加格式。段落来自另一个数据库来源,并且可能在每个文档中成百上千。

 


 

这会做到的...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    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

posted @ 2026-10-29 19:28  1010阿龙  阅读(3)  评论(0)    收藏  举报