Harry88
03-07-2023, 12:02 AM
I need to develop a macro to count occurrences of words in a document, and at the same time to apply coloured highlighting to them.
The prototype below counts six words listed in an array ("apple", "orange", "carrot", "potato", "corn", "wheat") and displays the totals in a message box.
Sub Macro1()
Dim vFindText As Variant
Dim oRng As Range
Dim i As Integer, n(6) As Integer
vFindText = Array("apple", "orange", "carrot", "potato", "corn", "wheat")
For i = 0 To UBound(vFindText)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = vFindText(i)
Do While .Execute
n(i) = n(i) + 1
' Add code here to apply highlighting?
oRng.Collapse 0
Loop
End With
Next i
lbl_Exit:
MsgBox _
"Fruit:" & vbCr & _
" " & n(0) & " apple" & vbCr & _
" " & n(1) & " orange" & vbCr & vbCr & _
"Vegetable:" & vbCr & _
" " & n(2) & " carrot" & vbCr & _
" " & n(3) & " potato" & vbCr & vbCr & _
"Grain:" & vbCr & _
" " & n(4) & " corn" & vbCr & _
" " & n(5) & " wheat" _
, vbInformation
Exit Sub
End Sub
How can I get it to apply coloured highlighting at the same time; for example, turquoise for fruit (i.e., "apple" or "orange", when i = 1 or 2), pink for vegetable (i.e., "carrot" or "potato", when i = 3 or 4), and green for grain (i.e., "corn" or "wheat", when i = 5 or 6)?
I wondered whether it is possible to add something inside the Do loop, along the following lines, but I don't know the proper code for applying highlighting.
If i = 1 Or 2 Then [Apply highlight wdTurquoise]
Else If i = 3 Or 4 Then [Apply highlight wdPink]
Else If i = 5 Or 6 Then [Apply highlight wdGreen]
The counting works OK on the attached "Test before" document. The attached "Test after" document shows how it should look after the macro is run.
30601
30602
The prototype below counts six words listed in an array ("apple", "orange", "carrot", "potato", "corn", "wheat") and displays the totals in a message box.
Sub Macro1()
Dim vFindText As Variant
Dim oRng As Range
Dim i As Integer, n(6) As Integer
vFindText = Array("apple", "orange", "carrot", "potato", "corn", "wheat")
For i = 0 To UBound(vFindText)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = vFindText(i)
Do While .Execute
n(i) = n(i) + 1
' Add code here to apply highlighting?
oRng.Collapse 0
Loop
End With
Next i
lbl_Exit:
MsgBox _
"Fruit:" & vbCr & _
" " & n(0) & " apple" & vbCr & _
" " & n(1) & " orange" & vbCr & vbCr & _
"Vegetable:" & vbCr & _
" " & n(2) & " carrot" & vbCr & _
" " & n(3) & " potato" & vbCr & vbCr & _
"Grain:" & vbCr & _
" " & n(4) & " corn" & vbCr & _
" " & n(5) & " wheat" _
, vbInformation
Exit Sub
End Sub
How can I get it to apply coloured highlighting at the same time; for example, turquoise for fruit (i.e., "apple" or "orange", when i = 1 or 2), pink for vegetable (i.e., "carrot" or "potato", when i = 3 or 4), and green for grain (i.e., "corn" or "wheat", when i = 5 or 6)?
I wondered whether it is possible to add something inside the Do loop, along the following lines, but I don't know the proper code for applying highlighting.
If i = 1 Or 2 Then [Apply highlight wdTurquoise]
Else If i = 3 Or 4 Then [Apply highlight wdPink]
Else If i = 5 Or 6 Then [Apply highlight wdGreen]
The counting works OK on the attached "Test before" document. The attached "Test after" document shows how it should look after the macro is run.
30601
30602