对于字符序列中的文本,可以使用3种方法设置文本的颜色,包括RGB着色、索引着色和用主题颜色着色等。[大谦Excel,dqexcel点com]
RGB着色
进行RGB着色,需要首先获取Range对象的TextColor属性,它返回一个ColorFormat对象,通过设置ColorFormat对象的RGB属性的值进行RGB着色。给RGB属性赋值时,可以有4种赋值方式,包括指定给定颜色的红色、绿色和兰色分量、设置一个十六进制颜色值、一个表示颜色的常数和一个表示颜色的整数或者。
下面的代码用RGB函数指定给定颜色的红色、绿色和兰色分量,各分量在0-255之间取值。
Sub Test()
'RGB着色
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim rng As Range
Set rng = doc.Range(Start:=0, End:=20)
Dim fnt As Font
Set fnt = rng.Font
fnt.TextColor.RGB = RGB(0, 255, 0)
End Sub
效果如图3-7所示。
图3-7
下面定义文档中的一个字符序列,获取其Font对象,用它的TextColor属性获取ColorFormat对象,设置其RGB属性的值为&HFF&。这是一个十六进制数,表示的颜色为红色。
Sub Test4()
'用Font对象的属性进行设置
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim rng As Range
Set rng = doc.Range(Start:=0, End:=20)
Dim fnt As Font
Set fnt = rng.Font
fnt.TextColor.RGB = &HFF&
End Sub
设置效果如图3-8所示,表3-1中列出了很多常见颜色的16进制数。
图3-8 设置字符序列的颜色为红色
进行RGB着色的第3种方式是将RGB属性的值设置为表示指定颜色的常数,如下面wdColorRed表示红色。表3-1中列出了表示常见颜色的常数。
Sub Test3()
'用Font对象的属性进行设置
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim rng As Range
Set rng = doc.Range(Start:=0, End:=20)
Dim fnt As Font
Set fnt = rng.Font
fnt.TextColor.RGB = wdColorViolet
End Sub
设置效果如图3-9所示。
图3-9
进行RGB着色的第4种方式是将RGB属性的值设置为表示指定颜色的整数,如下面255表示红色。表3-1中列出了表示常见颜色的整数。
Sub Test4()
'用Font对象的属性进行设置
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim rng As Range
Set rng = doc.Range(Start:=0, End:=20)
Dim fnt As Font
Set fnt = rng.Font
fnt.TextColor.RGB = 26367
End Sub
设置效果如图3-10所示。
图3-10
表3-1 常见颜色的16进制数、常数、整数和RGB分量表示
| 颜色 | 常数名称 | 16进制数 | 整数 | R | G | B |
|---|---|---|---|---|---|---|
| 黑色 | wdColorBlack | &H000000& | 0 | 0 | 0 | 0 |
| 兰色 | wdColorBlue | &H0000FF& | 16711680 | 0 | 0 | 255 |
| 深红色 | wdColorBrown | &H993300& | 13209 | 153 | 51 | 0 |
| 绿色 | wdColorGreen | &H008000& | 32768 | 0 | 128 | 0 |
| 橙色 | wdColorOrange | &HFF6600& | 26367 | 255 | 102 | 0 |
| 粉色 | wdColorPink | &HFF00FF& | 16711935 | 255 | 0 | 255 |
| 红色 | wdColorRed | &HFF0000& | 255 | 255 | 0 | 0 |
| 青色 | wdColorTeal | &H008080& | 8421376 | 0 | 128 | 128 |
| 紫色 | wdColorViolet | &H800080& | 8388736 | 128 | 0 | 128 |
| 白色 | wdColorWhite | &HFFFFFF& | 16777215 | 255 | 255 | 255 |
| 黄色 | wdColorYellow | &HFFFF00& | 65535 | 255 | 255 | 0 |
索引着色
索引着色是先给定一个颜色查找表,如表3-2所示,表中给定一些颜色,每个颜色有索引号和常数表示,使用索引着色时直接指定颜色为颜色查找表中某个颜色的索引号。
表3-2 颜色查找表
| 颜色 | 索引号 | 常数 | 颜色 | 索引号 | 常数 |
|---|---|---|---|---|---|
| 自动配色 | 0 | wdAuto | 深蓝色 | 9 | wdDarkBlue |
| 黑色 | 1 | wdBlack | 青色 | 10 | wdTeal |
| 蓝色 | 2 | wdBlue | 绿色 | 11 | wdGreen |
| 青绿色 | 3 | wdTurquoise | 紫色 | 12 | wdViolet |
| 鲜绿色 | 4 | wdBrightGreen | 深红色 | 13 | wdDarkRed |
| 粉红色 | 5 | wdPink | 深黄色 | 14 | wdDarkYellow |
| 红色 | 6 | wdRed | 50% 灰色底纹 | 15 | wdGray50 |
| 黄色 | 7 | wdYellow | 25% 灰色底纹 | 16 | wdGray25 |
| 白色 | 8 | wdWhite | 清除 | 0 | wdNoHighlight |
给Range对象中文本的内容进行索引着色,设置其Font属性返回的Font对象的ColorIndex属性的值为表3-2 中某个颜色的索引值或常数值即可。
Sub Test5()
'索引着色
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim rng As Range
Set rng = doc.Range(Start:=0, End:=20)
Dim fnt As Font
Set fnt = rng.Font
fnt.ColorIndex = wdBlue
End Sub
设置效果如图3-7所示。
使用主题颜色
Word提供了10余种主题颜色,如表3-3中所示。使用这些主题颜色,可以很方便地给图形着色。
表3-3 Word中的主题颜色
| 常 数 | 值 | 颜 色 |
|---|---|---|
| wdNotThemeColor | -1 | 无颜色 |
| WdThemeColorMainDark1 | 0 | 深色主色1 |
| wdThemeColorMainLight1 | 1 | 浅色主色1 |
| wdThemeColorMainDark2 | 2 | 深色主色2 |
| wdThemeColorMainLight2 | 3 | 浅色主色2 |
| wdThemeColorAccent1 | 4 | 强调文字颜色1 |
| wdThemeColorAccent2 | 5 | 强调文字颜色2 |
| wdThemeColorAccent3 | 6 | 强调文字颜色3 |
| wdThemeColorAccent4 | 7 | 强调文字颜色4 |
| wdThemeColorAccent5 | 8 | 强调文字颜色5 |
| wdThemeColorAccent6 | 9 | 强调文字颜色6 |
| wdThemeColorHyperlink | 10 | 超链接颜色 |
| wdThemeColorHyperlinkFollowed | 11 | 已访问的超链接颜色 |
| wdThemeColorBackground1 | 12 | 背景色1 |
| wdThemeColorText1 | 13 | 文本颜色1 |
| wdThemeColorBackground2 | 14 | 背景色2 |
| wdThemeColorText2 | 15 | 文本颜色2 |
给Range对象中文本的内容用主题颜色进行着色,其Font属性返回Font对象,用Font对象的TextColor属性返回ColorFormat对象,将该对象的ObjectThemeColor属性的值设置为表11-3中某个颜色的值或常数。
Sub Test6()
'主题颜色着色
Dim doc As Document
Set doc = Documents.Open("D:\test2.docx")
Dim rng As Range
Set rng = doc.Range(Start:=0, End:=20)
Dim fnt As Font
Set fnt = rng.Font
fnt.TextColor.ObjectThemeColor = wdThemeColorMainDark2
End Sub
将定义字符序列内的文字设置为兰色。