Advertisement
Guest User

VBA word words hightlight

a guest
Jul 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub HighlightWords()
  2. Dim WORDS() As String
  3. ReDim Preserve WORDS(0)
  4. '
  5. AddStringValue WORDS, "тип"
  6. AddStringValue WORDS, "должен"
  7. '
  8. For Each sentence In ActiveDocument.StoryRanges
  9.     For Each w In sentence.WORDS
  10.      If IsInArray(LCase(Trim(w.Text)), WORDS) Then
  11.         ActiveDocument.Range(w.Start, w.End - 1).HighlightColorIndex = wdYellow
  12.      End If
  13.  Next
  14. Next
  15. End Sub
  16.  
  17. Private Sub AddStringValue(ByRef Arr As Variant, ByVal Value As String)
  18. If UBound(Arr) = 0 And Len(Arr(0)) = 0 Then
  19.     Arr(0) = Value
  20. Else
  21.     ReDim Preserve Arr(UBound(Arr) + 1)
  22.     Arr(UBound(Arr)) = Value
  23. End If
  24. End Sub
  25.  
  26. Private Function IsInArray(valToBeFound As Variant, Arr As Variant) As Boolean
  27. Dim element As Variant
  28. On Error GoTo IsInArrayError: 'array is empty
  29.    For Each element In Arr
  30.         If element = valToBeFound Then
  31.             IsInArray = True
  32.             Exit Function
  33.         End If
  34.     Next element
  35. Exit Function
  36. IsInArrayError:
  37. On Error GoTo 0
  38. IsInArray = False
  39. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement