|
朋友,下次引用我写的代码,麻烦你还是注明一下“代码的出处”!
这次我再写一下你这个需求,详见如下代码:
- Sub test()
- Dim findtext As String, n As Integer, k, x As Long, a() As Long, p As Range
- findtext = InputBox("关键词" & vbCr & "多关键词格式:风,万" & vbCr & "一个关键词格式:风", , "刀,风")
- ActiveDocument.Content.HighlightColorIndex = wdNoHighlight
- If findtext = Empty Then Exit Sub
- k = Split(findtext, ","): n = UBound(k)
- With ActiveDocument.Content.Find
- Do While .Execute("^13{2,}", , , -1)
- With .Parent
- m = m + 1: ReDim Preserve a(m)
- a(m) = .Start: .Start = .End
- End With
- Loop
- End With
- a(m) = ActiveDocument.Range.End: a(0) = 0
- For i = 1 To m
- Set p = ActiveDocument.Range(a(i - 1), a(i)).Duplicate
- With ActiveDocument.Range(a(i - 1), a(i)).Find
- Do While .Execute(k(n))
- With .Parent
- If Not .InRange(p) Then Exit Do
- .Expand wdSentence
- .MoveEndWhile vbCr, wdBackward
- If f(k, .Text, n) Then
- If .Text Like "*[。!?……]" Then
- p.HighlightColorIndex = wdYellow
- x = x + 1: Exit Do
- End If
- End If
- .Start = .End
- End With
- Loop
- End With
- Next
- MsgBox IIf(x > 0, "找到:" & x & "首诗并标记!", "没有找到符合要求的!")
- End Sub
- Function f(k, ByVal sr As String, ByVal n As Integer) As Boolean
- Dim x As Integer
- If n = 0 Then f = True: Exit Function
- Do While x <= n - 1
- If InStr(sr, k(x)) = 0 Then Exit Do
- x = x + 1
- Loop
- If x = n Then f = True
- End Function
复制代码 |
|