LoginSignup
1
1

More than 3 years have passed since last update.

Word VBAのColorIndexを可視化してみた

Posted at

翻訳用のWordマクロをつくっていると、変更した箇所を際立たせるため、ColorIndexをよく使います。

ところが、マイクロソフト公式のドキュメントはなんと白黒!
どの名前がどんな色なのか、思い出すのも面倒なので、思い切って可視化しました。

ColorIndex 色付き早見表

colorIndex.png

左からColorIndex(列挙体)の名前、値(Long)、説明となっています。
説明の部分にそれぞれ対応する色を付けました。

ColorIndex 色付けマクロ

この表があれば特に不要な気もしますが、コードも記載しておきます。
なお、VBAでは列挙体のキーを順番に取り出すことができなかったので、
表自体は上記のドキュメントからコピーして使っています。

Public Sub apply_colorIndex()

    Dim cTable  As Table
    Dim i As Integer

    Dim cIndex As String
    Dim index As Integer

    Set cTable = ThisDocument.Tables(1)

    For i = 2 To cTable.Rows.Count

        cIndex = cTable.Rows(i).Cells(2).Range.Text
        ' Wordの表組みでは、セルの最後に中黒のような記号(Chr(7))が入っているので
        ' これを削除したうえでInt型に変換する
        index = CInt(Replace(cIndex, Chr(7), ""))
        ' -1の wdByAuthorは設定していないとエラーになったのでスキップしておく
        If index > -1 Then
            cTable.Rows(i).Cells(3).Range.Font.colorIndex = index
            cTable.Rows(i).Cells(4).Range.HighlightColorIndex = index
        End If

    Next i

End Sub

フォントの色はRange.Fontのメンバーなのに、
蛍光ペンの色はRangeのメンバーだというのが少し納得いきませんが……

これでマクロで色を扱うのが少し楽になりそうです!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1