How can I use custom colours in the Microsoft VBA editor?

VBA reads the settings for the colors from these registry keys:

HKEY_CURRENT_USER\Software\Microsoft\VBA\6.0\Common\CodeForeColors HKEY_CURRENT_USER\Software\Microsoft\VBA\6.0\Common\CodeBackColors

Each of these keys contains a list of values (separated by space) for each entry inside Tools -> Options -> Editor Format. For example, the first value inside CodeBackColors is the background color and the first value inside CodeForeColors is the foreground color for normal text. The values are the internal id for the color, 0 means AUTO coloring, 11 shows cyan, 5 red etc.

To give it a try: Close Excel, change the first value of CodeBackColors to 5 and restart Excel. The background of the code editor will now be red.

The problem is that the VBA Editor does only support these internal values and the highest number it understands is 16. Any other value will not be accepted and it defaults back to AUTO.

I have tried several options of entering different values (e.g. 128255128, 16777215, #aaffee, 255 or "170,255,238") and none of them worked.

Therefore I think it is technically not possible to assign other colors.


I created an application based on the information found here: https://github.com/dimitropoulos/VBECustomColors

It basically backs up the VBE6/VBE7 .dll file and allows the use of custom colors without having to use an hex editor.

ss

You can download it here: https://github.com/gallaux/VBEThemeColorEditor

Enjoy

Edit: Source code is now available!


TeX HeX is spot on. However, it IS possible to change the 16 built-in colors with your own. It just requires a bit of hex editing. Here's a step-by-step guide. (Note: This is for VB6 SP6, file version 6.0.97.82).

  1. Backup your VB6.EXE file in your VB98 program folder. (Optional, but recommended).

  2. Fire up your favorite hex editor (shout out to HxD) and open VB6.EXE.

  3. Jump to address 0xE22F4. This is the start of the color table.

You should see four zeros. This represents the color black in RRGGBBAA format (alpha isn't supported so it's really just RRGGBB00 format). The next four bytes specify the next color and so on until you get to FFFFFF00 (white) ending at offset 0xE2333.

  1. Edit any of these four-byte values to your choosing. Just use the appropriate hex values in RGB format, followed by a zero byte. For example, RGB(64, 128, 192) would be 40 80 C0 00.

  2. Save your changes to the EXE and fire up VB6. You should see your new color(s) in the slots previously occupied by the built-in VB colors.