我对 Visual Basic for Applications 非常陌生,所以这个问题让我有点困惑。我想创建一个代码,它可以扫描 word 文档并将字母的颜色更改为分配给该特定字母的特定颜色,贯穿整个文档。例如,字母“A”和“a”将是深蓝色,而字母“B”和“b”将是深绿色。该代码有效,但是需要很长时间。我不知道代码是否可以改进得更快,因为我对此很陌生。我使用仅更改一个字母颜色的代码示例创建了此代码,我在这里找到了该代码(https://superuser.com/questions/230665/how-do-i-change-the-font-color-of- a-specific-letter-throughout-the-entire-ms-wor)。我创建的代码如下:
Sub ChangeLetterColor()
Const LETTERA = "A"
Const ALETTER = "a"
Const LETTERB = "B"
Const BLETTER = "b"
Const LETTERC = "C"
Const CLETTER = "c"
Const LETTERD = "D"
Const DLETTER = "d"
Const LETTERE = "E"
Const ELETTER = "e"
Const éLETTER = "é"
Const èLETTER = "è"
Const LETTERF = "F"
Const FLETTER = "f"
Const LETTERG = "G"
Const GLETTER = "g"
Const LETTERH = "H"
Const HLETTER = "h"
Const LETTERI = "I"
Const ILETTER = "i"
Const LETTERJ = "J"
Const JLETTER = "j"
Const LETTERK = "K"
Const KLETTER = "k"
Const LETTERL = "L"
Const LLETTER = "l"
Const LETTERM = "M"
Const MLETTER = "m"
Const LETTERN = "N"
Const NLETTER = "n"
Const LETTERO = "O"
Const OLETTER = "o"
Const òLETTER = "ò"
Const LETTERP = "P"
Const PLETTER = "p"
Const LETTERQ = "Q"
Const QLETTER = "q"
Const LETTERR = "R"
Const RLETTER = "r"
Const LETTERS = "S"
Const SLETTER = "s"
Const LETTERT = "T"
Const TLETTER = "t"
Const LETTERU = "U"
Const ULETTER = "u"
Const LETTERV = "V"
Const VLETTER = "v"
Const LETTERW = "W"
Const WLETTER = "w"
Const LETTERX = "X"
Const XLETTER = "x"
Const LETTERY = "Y"
Const YLETTER = "y"
Const LETTERZ = "Z"
Const ZLETTER = "z"
For i = 1 To ThisDocument.Range.Characters.Count
If ThisDocument.Range.Characters(i) = LETTERA Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkBlue
End If
If ThisDocument.Range.Characters(i) = ALETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkBlue
End If
If ThisDocument.Range.Characters(i) = LETTERB Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGreen
End If
If ThisDocument.Range.Characters(i) = BLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGreen
End If
If ThisDocument.Range.Characters(i) = LETTERC Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTurquoise
End If
If ThisDocument.Range.Characters(i) = CLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTurquoise
End If
If ThisDocument.Range.Characters(i) = LETTERD Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGray25
End If
If ThisDocument.Range.Characters(i) = DLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGray25
End If
If ThisDocument.Range.Characters(i) = LETTERE Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTeal
End If
If ThisDocument.Range.Characters(i) = ELETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTeal
End If
If ThisDocument.Range.Characters(i) = éLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTeal
End If
If ThisDocument.Range.Characters(i) = èLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTeal
End If
If ThisDocument.Range.Characters(i) = LETTERF Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkYellow
End If
If ThisDocument.Range.Characters(i) = FLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkYellow
End If
If ThisDocument.Range.Characters(i) = LETTERG Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdRed
End If
If ThisDocument.Range.Characters(i) = GLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdRed
End If
If ThisDocument.Range.Characters(i) = LETTERH Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdViolet
End If
If ThisDocument.Range.Characters(i) = HLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdViolet
End If
If ThisDocument.Range.Characters(i) = LETTERI Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdBrightGreen
End If
If ThisDocument.Range.Characters(i) = ILETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdBrightGreen
End If
If ThisDocument.Range.Characters(i) = LETTERJ Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkRed
End If
If ThisDocument.Range.Characters(i) = JLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkRed
End If
If ThisDocument.Range.Characters(i) = LETTERK Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkYellow
End If
If ThisDocument.Range.Characters(i) = KLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkYellow
End If
If ThisDocument.Range.Characters(i) = LETTERL Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGreen
End If
If ThisDocument.Range.Characters(i) = LLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGreen
End If
If ThisDocument.Range.Characters(i) = LETTERM Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdPink
End If
If ThisDocument.Range.Characters(i) = MLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdPink
End If
If ThisDocument.Range.Characters(i) = LETTERN Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGray50
End If
If ThisDocument.Range.Characters(i) = NLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGray50
End If
If ThisDocument.Range.Characters(i) = LETTERO Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdYellow
End If
If ThisDocument.Range.Characters(i) = OLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdYellow
End If
If ThisDocument.Range.Characters(i) = òLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdYellow
End If
If ThisDocument.Range.Characters(i) = LETTERP Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdBlack
End If
If ThisDocument.Range.Characters(i) = PLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdBlack
End If
If ThisDocument.Range.Characters(i) = LETTERR Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdViolet
End If
If ThisDocument.Range.Characters(i) = RLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdViolet
End If
If ThisDocument.Range.Characters(i) = LETTERS Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdRed
End If
If ThisDocument.Range.Characters(i) = SLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdRed
End If
If ThisDocument.Range.Characters(i) = LETTERT Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkBlue
End If
If ThisDocument.Range.Characters(i) = TLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdDarkBlue
End If
If ThisDocument.Range.Characters(i) = LETTERU Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTurquoise
End If
If ThisDocument.Range.Characters(i) = ULETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdTurquoise
End If
If ThisDocument.Range.Characters(i) = LETTERV Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGray25
End If
If ThisDocument.Range.Characters(i) = VLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGray25
End If
If ThisDocument.Range.Characters(i) = LETTERW Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdPink
End If
If ThisDocument.Range.Characters(i) = WLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdPink
End If
If ThisDocument.Range.Characters(i) = LETTERX Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdYellow
End If
If ThisDocument.Range.Characters(i) = XLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdYellow
End If
If ThisDocument.Range.Characters(i) = LETTERY Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGreen
End If
If ThisDocument.Range.Characters(i) = YLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdGreen
End If
If ThisDocument.Range.Characters(i) = LETTERZ Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdBlack
End If
If ThisDocument.Range.Characters(i) = ZLETTER Then
ThisDocument.Range.Characters(i).Font.ColorIndex = wdBlack
End If
Next
End Sub
如果这很糟糕,我很抱歉,但我只是一个初学者,如果有人可以帮助我,将不胜感激。当我在启用宏的 word 文档中运行此代码时,仅写一页就需要一个多小时,我已经在多台计算机上尝试过。