VBA 每个段落的单词编号从 1 开始

VBA Word Numbering starting at 1 for every paragraph

我有一个 Sub 为每个 wdListOutlineNumbering 设置特定的样式。我的问题是编号是递增的,我想每个段落都从 1 开始。

到目前为止我的代码:

Sub SetNumberingStyle()
    Dim para As Paragraph
    For Each para In ActiveDocument.Paragraphs
        If para.Range.ListFormat.ListType = wdListOutlineNumbering Then
            para.Style = "List Number"
            ListGalleries(wdNumberGallery).ListTemplates(4).Name = ""
            para.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
            ListGalleries(wdNumberGallery).ListTemplates(4),
            ContinuePreviousList:= _
            False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
            wdWord10ListBehavior
        End If
    Next para
End Sub

这不会将编号重置为从 1 开始。 我的问题是我必须在代码中更改什么。

根据您的描述,您需要执行一个两步过程:

首先,将列表编号样式应用于所有需要的段落。您将不得不弄清楚如何执行此操作,因为您对文档的描述含糊不清。

其次,找到使用列表编号格式化的段落,并在适用的地方重新开始编号。我假设在列表编号样式中有连续 运行 的段落,您希望它们被连续编号。 (你的问题似乎表明你希望每个List Number para都编号为1,我觉得这不合逻辑)

Sub RestartNumbering()

    Dim listTemp As ListTemplate
    Set listTemp = ActiveDocument.Styles(wdStyleListNumber).ListTemplate
   
    With ActiveDocument.Range
        With .Find
            .Format = True
            .Text = ""
            .Style = wdStyleListNumber
        End With
        Do While .Find.Execute
            If Not .Previous(wdParagraph).Style = .Style Then
                .ListFormat.ApplyListTemplateWithLevel _
                    ListTemplate:=listTemp, _
                    ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
                    DefaultListBehavior:=wdWord10ListBehavior
            End If
        Loop
    End With
End Sub

当然可以。如果您使用的大纲编号已正确设置,则无需执行此操作。