Reputation: 9
Would it be possible to count color word inside a Word document? Let say there are two color word in my document. I need to count the word that is the color Blue and I need to count the word that is the color Red.
I only found Count words in a Microsoft Word document by Fonts
Sub CountTypeface()
Dim lngWord As Long
Dim lngCountIt As Long
Const Typeface As String = "Cambria"
For lngWord = 1 To ActiveDocument.Words.Count
'Ignore any document "Words" that aren't real words (CR, LF etc)
If Len(Trim(ActiveDocument.Words(lngWord))) > 1 Then
If ActiveDocument.Words(lngWord).Font.Name = Typeface Then
lngCountIt = lngCountIt + 1
End If
End If
Next lngWord
MsgBox "Number of " & Typeface & " words: " & lngCountIt
End Sub
Upvotes: 1
Views: 1768
Reputation: 43575
Try with this:
Option Explicit
Sub CountTypeface()
Dim lngWord As Long
Dim lngCountIt As Long
Const ColorIndex As Long = 6
For lngWord = 1 To ActiveDocument.Words.Count
If Len(Trim(ActiveDocument.Words(lngWord))) > 1 Then
Debug.Print ActiveDocument.Words(lngWord).Font.ColorIndex
If ActiveDocument.Words(lngWord).Font.ColorIndex = ColorIndex Then
lngCountIt = lngCountIt + 1
End If
End If
Next lngWord
MsgBox "Number of colored words: " & lngCountIt
End Sub
6 is for red. If you put a small text in Word and you color a few words, it would print them their colors in the immediate window, before giving you the messagebox. Thus, you would learn the number of the colors.
Upvotes: 1