VBA按颜色计算Microsoft Word文档中的字数
是否有可能计算word文档中的颜色词。假设我的文档中有两个颜色词。我需要数一数单词的颜色是蓝色,我需要数一数单词的颜色是红色 我只找到“按字体计算Microsoft Word文档中的字数” 使用以下脚本: 子字体() 暗淡的长的 暗算得尽可能长 常量字体为String=“Cambria” 端接头 请给我一些建议 谢谢。试试这个:VBA按颜色计算Microsoft Word文档中的字数,vba,count,ms-word,Vba,Count,Ms Word,是否有可能计算word文档中的颜色词。假设我的文档中有两个颜色词。我需要数一数单词的颜色是蓝色,我需要数一数单词的颜色是红色 我只找到“按字体计算Microsoft Word文档中的字数” 使用以下脚本: 子字体() 暗淡的长的 暗算得尽可能长 常量字体为String=“Cambria” 端接头 请给我一些建议 谢谢。试试这个: Option Explicit Sub CountTypeface() Dim lngWord As Long Dim lngCountIt As L
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代表红色。如果你在Word中输入一个小文本并给几个单词上色,它会在给你消息框之前在即时窗口中打印它们的颜色。因此,您可以了解颜色的数量。谢谢Vitaya,它可以工作。至于颜色的数量,你能给我建议一下我可以参考的网页吗。非常感谢。最好的选择就是自己做一个检查,只写一个单词,然后在运行时看到它的颜色。它将在即时窗口中打印。否则,对于页面来说有点难,但是试试看
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