如何通过MS Word mcro查找段落结尾(:)字符

时间:2016-03-17 19:38:42

标签: vba ms-word word-vba

我有一个工作宏,可以找到一些段落的样式。我需要找到段落结尾(:)字符。我试过下面的代码。但这不起作用:(

If doc.Paragraphs(i).Find.Text = ":^p" Then

我不知道在此代码中将其添加到何处。有人能帮我吗? 请参阅以下完整代码:

Sub CheckKeepWithNextForColonParas()

Const message As String = "Check Keep With Next"
Const styleMask As String = "(:) + KWN False"
Dim paragraphCount As Integer
Dim i As Integer
Dim currentStyle As String
Set doc = ActiveDocument

paragraphCount = doc.Paragraphs.count

Do While i < paragraphCount
    i = i + 1
    If doc.Paragraphs(i).Find.Text = ":^p" Then
    If doc.Paragraphs(i).KeepWithNext = False Then
    If doc.Paragraphs(i).Range.Tables.count = 0 Then

        currentStyle = doc.Paragraphs(i).Range.Style

        If Left(currentStyle, Len(styleMask)) <> styleMask Then

            doc.Paragraphs(i).Range.Select
            Selection.Comments.Add Range:=Selection.Range
            Selection.TypeText Text:=message
          End If
        End If
      End If
    End If

Loop
Set doc = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个

Sub Demo123()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ":^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = False
    .Execute
  End With
  Do While .Find.Found
      .HighlightColorIndex = wdGreen
      'or do whatever you want
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

或试试这个

Sub Colorgreenfromw()
Application.ScreenUpdating = False
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Paragraphs
    Set oRng = oPar.Range
    With oRng
        With .Find
            .ClearFormatting
            .Font.Color = wdColorGreen
            .Replacement.ClearFormatting
            .Text = ":^13"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = False
            .Execute
        End With
            If .Find.Found Then
             Set oRng = oPar.Range
                oRng.Font.Color = wdColorGreen
             Set oRng = Nothing
            End If
    End With
Next
End Sub