找到以"定理证明"开头的段落。

时间:2017-11-07 02:45:39

标签: vba word-vba

我想用红色标记以"定理证明"开头的段落。

我的代码如下:

Sub theorem()
    Dim p As Paragraph, d As Document
        For Each p In ActiveDocument
        If p.Range.Words(1) = "Proof " And p.range.words(2) = "of " and p.Range.Words(3) = "theorem " Then
                End If
    End Sub

我觉得这种方法“If p.Range.Words(1) = "Proof " And p.range.words(2) = "of " and p.Range.Words(3) = "theorem "”很笨重。我想问一下是否有更简洁的方法或其他任何提示。

1 个答案:

答案 0 :(得分:1)

您可以使用Left函数检索段落的前16个字符并测试:

Sub theorem()
    Dim p As Paragraph, d As Document
    For Each p In ActiveDocument.Paragraphs
        If Left(p.Range.Text, 16) = "Proof of theorem" Then
            p.Range.Font.ColorIndex = wdRed
        End If
    Next
End Sub