0

私は Visual Basic for Applications を初めて使用するので、この問題に少し戸惑っています。Word 文書をスキャンし、文書全体で文字の色をその特定の文字に割り当てられた特定の色に変更するコードを作成したかったのです。たとえば、文字 'A' と 'a' は濃い青色になり、文字 'B' と 'b' は濃い緑色になります。コードは機能しますが、非常に時間がかかります。私はこれに非常に慣れていないため、コードを改善して高速化できるかどうかはわかりません。ここで見つけた 1 文字の色だけを変更するコードの例を使用して、このコードを作成しました ( https://superuser.com/questions/230665/how-do-i-change-the-font-color-of- a-specific-letter-through-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 文書でこのコードを実行すると、1 ページの書き込みを行うだけで 1 時間以上かかります。これを複数のコンピューターで試しました。

4

2 に答える 2