Word:删除样式后的任意第一段'Heading 2'

Word: Delete any first paragraph after the style 'Heading 2'

我知道很多编程语言,但没有更改非常大的 Word 文件的经验。 请帮忙。这将是巨大的帮助!

我可以通过宏 VBA 或 Apache.POI 来实现吗?我的第一次尝试是 VBA(伪代码),见下文。

需求一:如何直接删除'heading 2'样式后的第一段?

要求2:要删除的段落必须以数字开头。

要求 3:段落应包含斜体文本。并具有样式 'normal' 或标准。

这应该是这样的(伪代码):

Sub DeleteParagraphAfterHeading2StaringWithNumberBeingItalics()
    heading2Found = False
    Dim para As Paragraph
    For Each para In ActiveDocument.Paragraphs
        If para.Style = wdStyleHeading2
           heading2Found = True
        ElseIf heading2Found = True Then            
            txt = para.Range.Text
            If ( para.Style = wdStyleNormaltext ) And  _
               ( txt.startsWith( number) ) And  _
               ( para.Range.Font.Italic = True) Then
                para.Range.Delete
            End If
            heading2Found = False
       Else 
            heading2Found = False
       End if
       Next para
End Sub

手动执行此操作需要很多天。所以,如果你能帮忙,

我认为代码可能是这样的(需要在具有真实本地化样式的真实文本上进行调试):

Sub del_para()
    With ActiveDocument.Range
        .Find.ClearFormatting
        .Find.Style = ActiveDocument.Styles("Heading 2")    ' adjust style name
        Do
            If .Find.Execute Then   'find by style
                .Move Unit:=wdParagraph
                .Expand Unit:=wdParagraph
                If (.ListFormat.ListType = wdListOutlineNumbering _
                    Or .ListFormat.ListType = wdListSimpleNumbering _
                    Or Left(.Text, 1) Like "[0-9]") _
                    And .Style = ActiveDocument.Styles("normal") _
                    And .Font.Italic Then
                    .Font.ColorIndex = wdRed    ' for debug
                    '.Delete
                End If
            Else
                Exit Do
            End If
            .Collapse wdCollapseEnd
        Loop
    End With
End Sub

例如:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Style = wdStyleHeading2
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
  End With
  Do While .Find.Execute
    With .Paragraphs.Last.Next.Range.Paragraphs.First.Range
      If .Style = wdStyleNormal Then
        If .Font.Italic = True Then
          If IsNumeric(Trim(.Words.First)) Then .Delete
        End If
      End If
    End With
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub