我想在一个由三个不同颜色的符号组成的文本中计数所有的点,它们不是索引颜色,而是服装RGB(例如,51,204,204)。然后想要显示每种颜色中有多少种颜色(例如,42种颜色和蓝色),所以想出了这一点,但没有编译,并说“无效限定符”指向".Font“,我在代码中突出显示了这一点:
Sub GetNumberOfBullets()
Dim objRange As Range
Dim objParagraph As Paragraph
Dim nNumber As Integer
' Initialization
Set objRange = Selection.Range
nNumber = 0
cyannum = 0
purplenum = 0
greennum = 0
For Each objParagraph In objRange.Paragraphs
If objParagraph.Range.ListFormat.ListType = WdListType.wdListBullet Then
nNumber = nNumber + 1
If objParagraph.Range`.Font`.Color.RGB = RGB(51, 204, 204) Then
cyannum = cyannum + 1
End If
If objParagraph.Range`.Font`.Color.RGB = RGB(204, 153, 255) Then
purplenum = purplenum + 1
End If
If objParagraph.Range`.Font`.Color.RGB = RGB(0, 176, 80) Then
greennum = greennum + 1
End If
End If
Next objParagraph
' Pop up a message box to show the total number of bullets.
MsgBox ("Bullet number:" & nNumber & "Cyan number:" & cyannum & "Purple number:" & purplenum & "Green number:" & greennum)
End Sub发布于 2017-06-01 10:45:41
编译器反对的不是Font,而是Color。在寻找Range的字体颜色时,您需要查看Fill属性。
如果您将代码调整为objParagraph.Range.Font.Fill.ForeColor.RGB,那么您将能够编译代码。
我从你的代码中看出,你在寻找紧跟在符号后面的文本的颜色,而不是符号字符?如果您正在寻找子弹字符的颜色,那么请参阅Variatus的评论。
发布于 2017-06-02 04:05:27
@Variatus的评论是绝对正确的。在编写代码之前,需要了解作者所做的工作,有多种方法可以对重点点进行着色。
通过突出一个段落并给文本着色,就可以给一个要点涂上颜色。要点将采用你最后一段的颜色(段落,字符)。如果段落包含多种颜色,那么Paragraph.Font.Color属性将返回“所有的9s”,所以您最好的选择是测试最后一个字符的颜色。
假设这是您想测试子弹点颜色的方式--这是一个很大的假设--那么代码的问题不在于Font属性,而在于RGB不是Color的属性。RGB()是一个从RGB值返回一个长的函数,因此您需要按照以下语法将Color值与RGB(r,b,g)进行比较:
If ... .Font.Color = RGB(r, b, g) Then
因此,您的更正代码如下所示:
Dim rng As Range
Dim para As Paragraph
Dim myCyan As Long, myPurple As Long, myGreen As Long
Dim cyanCount As Long, purpleCount As Long, greenCount As Long
myCyan = RGB(51, 204, 204)
myPurple = RGB(204, 153, 255)
myGreen = RGB(0, 176, 80)
Set rng = Selection.Range
For Each para In rng.Paragraphs
With para.Range
If .ListFormat.ListType = wdListBullet Then
Select Case .Characters.Last.Font.Color
Case myCyan: cyanCount = cyanCount + 1
Case myPurple: purpleCount = purpleCount + 1
Case myGreen: greenCount = greenCount + 1
End Select
End If
End With
Next
MsgBox "Cyan=" & cyanCount & ", " & _
"Purple=" & purpleCount & ", " & _
"Green=" & greenCounthttps://stackoverflow.com/questions/44296020
复制相似问题