如何通过MS Word宏查找多个段落属性



我有一个宏可以找到单词段落的一些属性。我需要使用宏找到"4 行或更多"段落。

我尝试了以下代码:

If oPar.LineCount = LineCount + 4 Then

有关完整代码,请参见下文:

Sub CheckKeepLinesTogether()
Application.ScreenUpdating = False
Const message As String = "Check Keep Lines Together"
Dim oPar As Paragraph
Dim oRng As Word.Range
Dim LineCount As Long
For Each oPar In ActiveDocument.Paragraphs
Set oRng = oPar.Range
With oRng
    With .Find
        .ClearFormatting
        .Text = "^13"
        .Execute
    End With
    Set oRng = oPar.Range
        If oPar.KeepTogether = False Then
        If oPar.LineCount = LineCount + 4 Then
            .Select
            Selection.Comments.Add Range:=Selection.Range
            Selection.TypeText Text:=message
            Set oRng = Nothing
        End If
      End If
    End With
 Next
 Application.ScreenUpdating = True
 End Sub

错误的行替换为未注释的代码:

    'If oPar.LineCount = LineCount + 4 Then
    If oPar.Range.ComputeStatistics(wdStatisticLines) >= 4 Then

顺便说一句,您无需设置Set oRng = oPar.Range两次。

未测试

Sub CheckKeepLinesTogether()
    Application.ScreenUpdating = False
    Const message As String = "Check Keep Lines Together"
    Dim oPar As Paragraph
    Dim oRng As Word.Range
    Dim LineCount As Long
    For Each oPar In ActiveDocument.Paragraphs
    Set oRng = oPar.Range
    With oRng
        With .Find
            .ClearFormatting
            .Text = "^13"
            .Execute
        End With
            If oPar.KeepTogether = False Then
            If oPar.Range.ComputeStatistics(wdStatisticLines) >= 4 Then
              Set oRng = oPar.Range
                oRng.Comments.Add Range:=oRng
                oRng.TypeText Text:=message
             Set oRng = Nothing
            End If
          End If
        End With
     Next
     Application.ScreenUpdating = True
     End Sub

最新更新