请修改一下代码,使代码满足这个要求,并输出完整代码: 1.从文本开头,开始查找第一个数字和第二个数字,并给第二个数字前面插入“/”,全选这两个数字之间的所有字符,并将第二个数字的颜色属性,应用上去。 4.查找第二个数字和第三个数字,并给第二个数字前面插入“/”,全选这两个数字之间的所有字符,并将第三个数字的颜色属性,应用上去,并给第二个数字前面插入“/”。 5. 因此类推, Sub ColorNumbers() Dim doc As Document Dim rng As Range Dim num As Variant Dim color As Variant Dim startpos As Long Dim endpos As Long Set doc = ActiveDocument Set rng = doc.Content With rng.Find .ClearFormatting .MatchWildcards = True .text = "[0-9]{1,}" .Forward = True .Wrap = wdFindStop Do While .Execute num = rng.text Select Case num Case 0 color = RGB(0, 0, 0) ' 黑色 Case 1 color = RGB(255, 0, 0) ' 红色 Case 2 color = RGB(255, 165, 0) ' 橙色 Case 3 color = RGB(255, 255, 0) ' 黄色 Case 4 color = RGB(0, 255, 0) ' 绿色 Case 5 color = RGB(139, 69, 19) ' 棕色 Case 6 color = RGB(0, 255, 255) ' 青色 Case 7 color = RGB(0, 0, 255) ' 蓝色 Case 8 color = RGB(128, 0, 128) ' 紫色 Case 9 color = RGB(255, 192, 203) ' 粉色 End Select With rng.Characters(1).font .color = color End With If startpos > 0 Then endpos = rng.Start rng.Parent.Range(startpos, endpos).font.color = rng.font.color End If startpos = rng.End Loop End With End Sub
时间: 2024-02-10 07:19:10 浏览: 79
Sub ColorNumbers()
Dim doc As Document
Dim rng As Range
Dim num As Variant
Dim color As Variant
Dim startpos As Long
Dim endpos As Long
Set doc = ActiveDocument
Set rng = doc.Content
With rng.Find
.ClearFormatting
.MatchWildcards = True
.Text = "([0-9]{1,}){2,}"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
num = rng.text
If startpos > 0 Then
endpos = rng.Start
rng.Parent.Range(startpos, endpos).InsertBefore "/"
rng.Parent.Range(startpos, endpos).Characters.Font.color = color
End If
Select Case num
Case Split(num, " ")(0)
color = RGB(0, 0, 0) ' 黑色
Case Split(num, " ")(1)
color = RGB(255, 0, 0) ' 红色
Case Split(num, " ")(2)
color = RGB(255, 165, 0) ' 橙色
Case Split(num, " ")(3)
color = RGB(255, 255, 0) ' 黄色
Case Split(num, " ")(4)
color = RGB(0, 255, 0) ' 绿色
Case Split(num, " ")(5)
color = RGB(139, 69, 19) ' 棕色
Case Split(num, " ")(6)
color = RGB(0, 255, 255) ' 青色
Case Split(num, " ")(7)
color = RGB(0, 0, 255) ' 蓝色
Case Split(num, " ")(8)
color = RGB(128, 0, 128) ' 紫色
Case Split(num, " ")(9)
color = RGB(255, 192, 203) ' 粉色
End Select
startpos = rng.End
Loop
If startpos > 0 Then
endpos = rng.Start
rng.Parent.Range(startpos, endpos).InsertBefore "/"
rng.Parent.Range(startpos, endpos).Characters.Font.color = color
End If
End With
End Sub
阅读全文