39

I'm working on a few projects for work using Excel and VBA, so I have no choice but to use the Microsoft Visual Basic editor. I normally wouldn't mind, but I can't figure out how to set a custom colour for the editor (I can only choose from the default palette).

To change the colour currently, I go to Tools -> Options -> Editor Format... However, your choices are only limited to the default (and basic) 16 colours - and when I say basic, I mean basic, like pink, blue, black, white, etc...

Does anyone know how to either specify a custom colour, or at the very least, change some of the colours that show up in the default palette?

1
  • I think what Microsoft is trying to tell us is that they don't want people to program in VBA. Or maybe Bill Gates are heavy into producers of pain killers.
    – Holene
    Jan 19, 2016 at 8:20

9 Answers 9

24
+50

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.

4
  • 1
    Thanks for the answer. IIRC, you can't withdraw a bounty, but if you literally cannot do this, then I shall award it to you for the most correct answer. I want to wait a bit more to see if any more answers are posted, but thanks for your response, +1. Jul 27, 2011 at 10:29
  • 1
    I have to admit, that sounds even better :)
    – Tex Hex
    Jul 27, 2011 at 10:42
  • "Total Visual CodeTools" (fmsinc.com/vb6/CodingTools.html) from FMS has a tool "VBE Color Scheme manager to easily see and set colors for your editor". If you download the user manual (a PDF) it shows screenshots that while making it easier to set colors is still limited to a 16 color palette. Would think if there was a direct way to use custom colors they would have included it in this tool. Looks like Tex Hex is correct.
    – Brian
    Aug 1, 2011 at 14:25
  • 1
    As it has now been shown with the below answers that it is possible , this answer should be modified. It is possible to reliably change the colours in the VBA Editor using a HEX editor. Jan 10, 2016 at 7:21
22

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!

8
  • Wow super cool, thanks for sharing @gallaux - I'll definitely check this out. Are you planning on releasing the source code out of curiousity? Nov 22, 2016 at 4:55
  • Yes I will soon, I just need to clean it up a little bit ;)
    – gallaux
    Nov 22, 2016 at 13:25
  • 1
    Sorry how apply this, i need help Jun 30, 2017 at 0:01
  • 2
    @YouCrackedMeUp you need to update the registry key using regedit (based on github.com/dimitropoulos/VBECustomColors): [1] Navigate in regedit to HKEY_CURRENT_USER\Software\Microsoft\VBA\7.1\Common. [2] change CodeBackColors to : 2 7 1 13 15 2 2 2 11 9 0 0 0 0 0 0. [3] change CodeForeColors to : 13 5 12 1 6 15 8 5 1 1 0 0 0 0 0 0. [4] change FontFace to : Consolas.
    – Tigregalis
    Aug 9, 2018 at 4:10
  • 1
    Very nice application. The theme keeps reverting on reboot. Is there a way to make the change permanent?
    – Friedrich
    May 3, 2021 at 8:23
7

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.

7
  • 1
    Hmm, I searched with the tool everything on a system with office 2003 and it doesn't find any file named VB6.exe or a folder VB98. Is this answer only for Office 2007 like the OP has? However the version 6.0.97.82 is older than mine with Office 2003.
    – nixda
    Feb 6, 2013 at 19:49
  • 1
    @nixda Nothing on my system either - I think Bond is referring to the VB6 application on his system, not the VBA integrated into Excel. I'm going to have a look through the Excel executable and some of the VBA DLLs, and see if I can find any colour tables. Feb 6, 2013 at 21:05
  • 1
    I have Office 2010 on my PC so I have VBE7.DLL, not VBE6.DLL, but it should be the same process. The colors are the same. If you look at the colors in the order they're displayed, they should be: Black = 00 00 00 00 Navy = 00 00 80 00 Green = 00 80 00 00 Turqoise = 00 80 80 00 Magenta = 80 00 00 00 Violet = 80 00 80 00 Olive = 80 80 00 00 Lgt Grey = C0 C0 C0 00 Drk Grey = 80 80 80 00 ... I've added the hex color codes next to each. So just open VBE6.DLL in a hex editor and search for the following hex string:
    – Bond
    Feb 12, 2013 at 16:13
  • 1
    00000000000080000080000000808000800000008000800080800000C0C0C00080808000 It may occur multiple times but I've found that the first color table is all that needs to be modified. See if that works for you.
    – Bond
    Feb 12, 2013 at 16:16
  • 1
    Awesome answer, Bond. For future readers, the key I needed to change was the second one (Office 2013, Windows 8.1), located at `12 50B4' using Hex Edit (Byte No. 1,200,308)
    – airstrike
    Nov 20, 2014 at 9:01
6

As many have noted, Bond's solution (hex editing the colors table in VB6.exe) will work but you will have to go in and reset your colors in the options dialog every time you start up VB6. I've created an AutoIt script that will do all of the work for you, just edit it as necessary where all of the calls to SetSyntaxColoring() are made:

Opt("WinTitleMatchMode", 2)
Opt("MouseClickDelay", 1)
Opt("MouseClickDownDelay", 1)
Opt("MouseClickDragDelay", 1)
Opt("SendKeyDelay", 1)
Opt("SendKeyDownDelay", 1)

Const $NORMALTEXT = 0
Const $SELECTIONTEXT = 1
Const $SYNTAXERRORTEXT = 2
Const $EXECUTIONPOINTTEXT = 3
Const $BREAKPOINTTEXT = 4
Const $COMMENTTEXT = 5
Const $KEYWORDTEXT = 6
Const $IDENTIFIERTEXT = 7
Const $BOOKMARKTEXT = 8
Const $CALLRETURNTEXT = 9

Const $AUTO = 0
Const $BLACK = 1
Const $DARKBLUE = 2
Const $DARKGREEN = 3
Const $DARKRED = 4
Const $DARKBABYBLUE = 5
Const $DARKPINK = 6
Const $DARKYELLOW = 7
Const $LIGHTGRAY = 8
Const $DARKGRAY = 9
Const $BLUE = 10
Const $GREEN = 11
Const $BABYBLUE = 12
Const $RED = 13
Const $PINK = 14
Const $YELLOW = 15
Const $WHITE = 16

If WinExists( "Microsoft Visual Basic" ) Then
   WinActivate("Microsoft Visual Basic") ; activates the visual basic window
   Send("!+to") ; opens the options menu
   Sleep(10)
   WinMove("Options", "", 0, 0) ; moves the options menu to the upper left corner of the screen
   MouseClick("primary", 100, 50, 1, 0) ; clicks the editor tab

   SetSyntaxColoring($NORMALTEXT, $WHITE, $BLACK)
   SetSyntaxColoring($SELECTIONTEXT, $AUTO, $AUTO)
   SetSyntaxColoring($SYNTAXERRORTEXT, $BLACK, $YELLOW)
   SetSyntaxColoring($EXECUTIONPOINTTEXT, $AUTO, $YELLOW)
   SetSyntaxColoring($BREAKPOINTTEXT, $WHITE, $DARKRED)
   SetSyntaxColoring($COMMENTTEXT, $DARKGREEN, $BLACK)
   SetSyntaxColoring($KEYWORDTEXT, $GREEN, $BLACK)
   SetSyntaxColoring($IDENTIFIERTEXT, $WHITE, $BLACK)
   SetSyntaxColoring($BOOKMARKTEXT, $WHITE, $BLACK)
   SetSyntaxColoring($CALLRETURNTEXT, $WHITE, $GREEN)

   MouseClick("primary", 210, 340, 1, 0) ; hit the ok button to apply the changes
EndIf

Func SetSyntaxColoring($syntax, $forecolor, $backcolor)
   SelectSyntax($syntax)
   SelectForecolor($forecolor)
   SelectBackcolor($backcolor)
EndFunc

Func SelectSyntax($syntax)
   ResetSyntaxList()
   RptSend("{DOWN}", $syntax)
EndFunc

Func SelectForecolor($color)
   ResetForecolorCbo()
   RptSend("{DOWN}", $color)
EndFunc

Func SelectBackcolor($color)
   ResetBackcolorCbo()
   RptSend("{DOWN}", $color)
EndFunc

Func ResetSyntaxList()
   MouseClick("primary", 50, 95, 1, 0)
   RptSend("{UP}", 9)
EndFunc

Func ResetForecolorCbo()
   MouseClick("primary", 50, 235, 1, 0)
   MouseClick("primary", 50, 235, 1, 0)
   RptSend("{UP}", 16)
EndFunc

Func ResetBackcolorCbo()
   MouseClick("primary", 125, 235, 1, 0)
   MouseClick("primary", 125, 235, 1, 0)
   RptSend("{UP}", 16)
EndFunc

Func RptSend($text, $rpt)
   For $i = 1 To $rpt
      Send($text)
   Next
EndFunc

I just keep it on my desktop and now whenever I have to open vb6 I just double click it and just like that syntax coloring is under my control.

Edit 1: Optimized the script a bit to make it run quicker. I am thinking about putting together a program that will edit VB6.EXE for you automatically to make color selection easier. I wonder if there's a way to ditch the AutoIt script by creating a plugin for VS?

Edit 2: Created a utility that allows you to edit the colors in the exe without having to use a hex editor: VbClassicColorEditor. That link goes to the public repository on bitbucket.

1
  • Thank you for the script and contribution! Any chance we could get an updated link on the VbClassicColorEditor? The one you posted has 404d...
    – airstrike
    Nov 20, 2014 at 7:48
4

I just wanted to post a summary of the previous answers as it's not entirely clear what needs to be done to change the colours in the Excel VBA Editor

In the below example I am adding the colour scheme for Solarized and i am assuming the use of Office 2010

Step 0: Make a backup of the VBE7.dll before modifying it - you have been warned!!!

Step 1: In a Hex Editor , open the VBE7.dll located @

"C:\Program Files\Common Files\microsoft shared\VBA\VBA7\VBE7.DLL" for 64bit

or

"C:\Program Files( x86)\Common Files\microsoft shared\VBA\VBA7\VBE7.DLL" for 32bit

Step 2: Find the 1st occurance of

ff ff ff 00 c0 c0 c0 00 80 80 80 00 00 00 00 00
ff 00 00 00 80 00 00 00 ff ff 00 00 80 80 00 00
00 ff 00 00 00 80 00 00 00 ff ff 00 00 80 80 00
00 00 ff 00 00 00 80 00 ff 00 ff 00 80 00 80 00`

and replace with

00 2b 36 00 07 36 42 00 58 6e 75 00 65 7b 83 00
83 94 96 00 93 a1 a1 00 ee e8 d5 00 fd f6 e3 00
b5 89 00 00 cb 4b 16 00 dc 32 2f 00 d3 36 82 00
6c 71 c4 00 26 8b d2 00 2a a1 98 00 85 99 00 00

Step 3: Find the 2nd occurance of this (ensure you search from top of file)

00 00 00 00 00 00 80 00 00 80 00 00 00 80 80 00
80 00 00 00 80 00 80 00 80 80 00 00 c0 c0 c0 00
80 80 80 00 00 00 ff 00 00 ff 00 00 00 ff ff 00
ff 00 00 00 ff 00 ff 00 ff ff 00 00 ff ff ff 00

and replace with

00 2b 36 00 07 36 42 00 58 6e 75 00 65 7b 83 00
83 94 96 00 93 a1 a1 00 ee e8 d5 00 fd f6 e3 00
b5 89 00 00 cb 4b 16 00 dc 32 2f 00 d3 36 82 00
6c 71 c4 00 26 8b d2 00 2a a1 98 00 85 99 00 00

Step 4: Save the dll file and then open Excel> VBA Editor > Tools> Options> Editor Format and set the code colours.

Changing both strings is nessecary as it allows for the colours to save correctly on shutdown and allows the colour editor to open with out excel crashing.

Thanks to all previous answerers for putting the time in to figuring this out!

4

I have created a program that automates all of this, spent far too long on it, had fun creating a colour picker. Made in VB6 because why not. Compiled program and source code available here. Tested on my versions of the DLL's and VB6 only, please make backups before changing anything - the locations are fixed in the code.



This is a very old question but I would like to weigh in for completeness

For VS2012 Colours in VBA or VB6 IDE: Open VBE6.DLL / VBE7.DLL or VBA6.DLL in

C:\Program Files (x86)\Common Files\microsoft shared\VBA\
or
C:\Program Files (x86)\VB98\ (Your VB6 install location)

with your hex editor

Replace the first occurrence of

ff ff ff 00 c0 c0 c0 00 80 80 80 00 00 00 00 00
ff 00 00 00 80 00 00 00 ff ff 00 00 80 80 00 00
00 ff 00 00 00 80 00 00 00 ff ff 00 00 80 80 00
00 00 ff 00 00 00 80 00 ff 00 ff 00 80 00 80 00

With

ff ff ff 00 1e 1e 1e 00 d4 d4 d4 00 00 00 00 00
ff 00 00 00 26 4f 78 00 ff ff 00 00 d6 9d 85 00
00 ff 00 00 60 8b 4e 00 00 ff ff 00 00 80 80 00
00 00 ff 00 56 9c d6 00 ff 00 ff 00 9f 74 b1 00

Then return to the top and replace the second occurrence of

00 00 00 00 00 00 80 00 00 80 00 00 00 80 80 00
80 00 00 00 80 00 80 00 80 80 00 00 c0 c0 c0 00
80 80 80 00 00 00 ff 00 00 ff 00 00 00 ff ff 00
ff 00 00 00 ff 00 ff 00 ff ff 00 00 ff ff ff 00

with

00 00 00 00 56 9c d6 00 60 8b 4e 00 d6 9d 85 00
26 4f 78 00 9f 74 b1 00 d6 9d 85 00 1e 1e 1e 00
d4 d4 d4 00 00 00 ff 00 00 ff 00 00 00 ff ff 00
ff 00 00 00 ff 00 ff 00 ff ff 00 00 ff ff ff 00

Then modify the following in the registry

(For VBA)
HKEY_CURRENT_USER\Software\Microsoft\VBA\6.0\Common    
HKEY_CURRENT_USER\Software\Microsoft\VBA\7.1\Common

(For VB6)
HKEY_CURRENT_USER\Software\Microsoft\VBA\Microsoft Visual Basic 

FontFace = 'Consolas'
CodeForeColors = '8 3 5 4 1 10 14 3 4 4 0 0 0 0 0 0 '
CodeBackColors = '2 6 4 8 10 2 2 2 16 10 11 12 13 14 15 0 '

If you are modifying for VBA you are done, for VB6 now open 'VB6.exe' in your Hex editor and modify the first occurrence of

00 00 00 00 00 00 80 00 00 80 00 00 00 80 80 00
80 00 00 00 80 00 80 00 80 80 00 00 c0 c0 c0 00
80 80 80 00 00 00 ff 00 00 ff 00 00 00 ff ff 00
ff 00 00 00 ff 00 ff 00 ff ff 00 00 ff ff ff 00

with

00 00 00 00 56 9c d6 00 60 8b 4e 00 d6 9d 85 00
26 4f 78 00 9f 74 b1 00 d6 9d 85 00 1e 1e 1e 00
d4 d4 d4 00 00 00 ff 00 00 ff 00 00 00 ff ff 00
ff 00 00 00 ff 00 ff 00 ff ff 00 00 ff ff ff 00

This corrects the colour displayed in the interface of VB6 so that if you wish to make other changes to the colours you can.

Thank you to all of the other answers, I didn't come up with this myself just thought it was useful to have all the information required in a single post (nowhere did I see modifying VB6.exe and VBA6.dll together). Using this method you should be able to freely select from the colours available.

I also reordered the colours from some of the other answers so not change vbRed, vbBlue, vbWhite etc so you should still get the desired outcome in code. This hasn't been fully tested so as always modify programs at your own risk.

It would be nice to put together a small program that will do this for you (like gallaux did), I had issues with writing to the registry and the VB6 IDE using that program, and come sort of preview window would be nice.

Limitations I have found: this does not change the colour of the indicator icons, you must do this yourself.

3

Taking the bait from dnissley's response (where he asks if someone could make an add-in), I've made an add-in for VB6. It's a little crude (and I'll explain why shortly), but it does do the job.

I created a new Add-In project in VB6 which gave me the default "frmAddin" form (which I don't use) and the "Connect" designer. I've added a Colour class myself which contains the following:

Option Explicit

Dim m_iForeColour As Integer
Dim m_iBackColour As Integer
Dim m_iIndicatorColour As Integer

Public Property Let ForeColour(ByVal iID As Integer)
    m_iForeColour = iID
End Property
Public Property Get ForeColour() As Integer
    ForeColour = m_iForeColour
End Property

Public Property Let BackColour(ByVal iID As Integer)
    m_iBackColour = iID
End Property
Public Property Get BackColour() As Integer
    BackColour = m_iBackColour
End Property

Public Property Let IndicatorColour(ByVal iID As Integer)
    m_iIndicatorColour = iID
End Property
Public Property Get IndicatorColour() As Integer
    IndicatorColour = m_iIndicatorColour
End Property

And then I've amended the code in the "Connect" Designer to be as follows:

Option Explicit

Public FormDisplayed          As Boolean
Public VBInstance             As VBIDE.VBE
Dim mcbMenuCommandBar         As Office.CommandBarControl
Dim mfrmAddIn                 As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents          'command bar event handler

Dim mcbToolbar As Office.CommandBarControl
Public WithEvents MenuHandler2 As CommandBarEvents

Dim codeColours() As Colour

'*****************************************************************************
' RunScript Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  Runs the code that sets the required colours for the code window in the
'  active IDE.
'  *** A PROJECT MUST BE LOADED BEFORE THIS WILL ACTUALLY WORK ***
'*****************************************************************************
Sub RunScript()
    ReadColoursFile

    ' Select Tools > Options
    SendKeys "%to", 5
    ' Go to tabs, select "Options"
    SendKeys "+{TAB}"
    SendKeys "{RIGHT}"

    ' Select listbox
    SendKeys "{TAB}"

    Dim colourSetting As Colour
    Dim iColour As Integer

    For iColour = 0 To 9
        SetColours iColour, codeColours(iColour)
    Next iColour

    SendKeys "~"
End Sub

'*****************************************************************************
' ReadColoursFile Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  Reads the colour file from disk and populates the codeColours array which
'  is used by the SetColour* methods for selecting the correct colours from
'  the options screen.
'*****************************************************************************
Sub ReadColoursFile()
    Dim colourLine As String
    Dim colourArray() As String
    Dim colourSetting As Colour
    Dim oFSO As FileSystemObject

    Set oFSO = New FileSystemObject

    If Not oFSO.FileExists(App.Path & "\VB6CodeColours.dat") Then
        MsgBox "VB6CodeColours.dat not found in " & App.Path, vbOKOnly, "VB6CodeColours Settings file not found!"
        Exit Sub
    End If

    Set oFSO = Nothing

    Open App.Path & "\VB6CodeColours.dat" For Input As #1
    ReDim codeColours(9) As Colour

    While Not EOF(1)
        Line Input #1, colourLine
        colourArray = Split(colourLine, ",")

        If IsNumeric(colourArray(0)) Then
            If codeColours(colourArray(0)) Is Nothing Then
                Set colourSetting = New Colour

                If IsNumeric(colourArray(1)) Then
                    colourSetting.ForeColour = CInt(colourArray(1))
                End If

                If IsNumeric(colourArray(2)) Then
                    colourSetting.BackColour = CInt(colourArray(2))
                End If

                If IsNumeric(colourArray(3)) Then
                    colourSetting.IndicatorColour = CInt(colourArray(3))
                End If

                Set codeColours(colourArray(0)) = colourSetting
            End If
        End If
    Wend

    Close #1

    Set colourSetting = Nothing
End Sub

'*****************************************************************************
' SetColours Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  Selects the colour item from the list and then iterates the colour selector
'  controls associated with that item and sets them according to the values
'  set in the VB6CodeColours.dat file.
'*****************************************************************************
Sub SetColours(ByVal iColour As Integer, ByRef colourSetting As Colour)
    Dim iKey As Integer

    SendKeys "{HOME}"

    For iKey = 1 To iColour
        SendKeys "{DOWN}"
    Next iKey

    SetColourSelector colourSetting.ForeColour
    SetColourSelector colourSetting.BackColour
    SetColourSelector colourSetting.IndicatorColour

    SendKeys "+{TAB}"
    SendKeys "+{TAB}"
    SendKeys "+{TAB}"
End Sub

'*****************************************************************************
' SetColourSelector Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  Sets the colour in the selector combo. Assumes the focus is on the
'  preceeding control before the code is run (first line tabs to the
'  assumed control).
'*****************************************************************************
Sub SetColourSelector(ByVal iColour As Integer)
    Dim iKey As Integer

    SendKeys "{TAB}"
    SendKeys "{HOME}"

    For iKey = 1 To iColour
        SendKeys "{DOWN}"
    Next iKey
End Sub

'*****************************************************************************
' AddinInstance_OnConnection Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  This method runs when the addin is loaded by the IDE
'*****************************************************************************
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
    On Error GoTo ErrorHandler

    'save the vb instance
    Set VBInstance = Application

    If ConnectMode  ext_cm_External Then
        Set mcbMenuCommandBar = AddToAddInCommandBar("VB6 Code Colouring")
        'sink the event
        Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)

        Dim oStdToolbar As Office.CommandBar
        Dim oStdToolbarItem As Office.CommandBarControl

        Set oStdToolbar = VBInstance.CommandBars("Standard")
        Set oStdToolbarItem = oStdToolbar.Controls.Add(Type:=msoControlButton)
        oStdToolbarItem.Style = msoButtonCaption
        oStdToolbarItem.Caption = "Set IDE Colours"
        oStdToolbarItem.BeginGroup = True
        Set Me.MenuHandler2 = VBInstance.Events.CommandBarEvents(oStdToolbarItem)
    End If

    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub

'*****************************************************************************
' AddinInstance_OnDisconnection Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  This method runs when the addin is removed by the IDE and cleans up any
'  references etc.
'*****************************************************************************
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
    On Error Resume Next

    'delete the command bar entry
    mcbMenuCommandBar.Delete

    'shut down the Add-In
    If FormDisplayed Then
        SaveSetting App.Title, "Settings", "DisplayOnConnect", "1"
        FormDisplayed = False
    Else
        SaveSetting App.Title, "Settings", "DisplayOnConnect", "0"
    End If

    Unload mfrmAddIn
    Set mfrmAddIn = Nothing

    Set MenuHandler = Nothing
    Set MenuHandler2 = Nothing
End Sub

'*****************************************************************************
' MenuHandler_Click Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  This method performs the tasks needed when the menu item is clicked.
'*****************************************************************************
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
    RunScript
End Sub

'*****************************************************************************
' MenuHandler2_Click Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  This method performs the tasks needed when the toolbar button is clicked.
'*****************************************************************************
Private Sub MenuHandler2_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
    RunScript
End Sub

'*****************************************************************************
' AddToAddInCommandBar Sub
'-----------------------------------------------------------------------------
' DESCRIPTION:
'  Adds the specified item to the menu list.
'*****************************************************************************
Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
    Dim cbMenuCommandBar As Office.CommandBarControl  'command bar object
    Dim cbMenu As Object

    On Error Resume Next

    'see if we can find the Add-Ins menu
    Set cbMenu = VBInstance.CommandBars("Add-Ins")
    If cbMenu Is Nothing Then
        'not available so we fail
        Exit Function
    End If

    On Error GoTo ErrorHandler

    'add it to the command bar
    Set cbMenuCommandBar = cbMenu.Controls.Add(1)
    'set the caption
    cbMenuCommandBar.Caption = sCaption

    Set AddToAddInCommandBar = cbMenuCommandBar

    Exit Function
ErrorHandler:
    ' Exit gracefully
End Function

This code allows the application to read the colours I want from a file that resides in the same directory as the .dll (called VB6CodeColours.dat). This file contains the following (and it will vary based on which colours you replace in the VB6.EXE so a straight copy & paste probably won't work.

0,14,12,0
1,0,0,0
2,16,13,0
3,0,15,15
4,16,5,5
5,7,12,0
6,11,12,0
7,8,12,0
8,16,10,10
9,16,3,3

Looks gibberish, but I'll explain. It has the format "Code Colour","Foreground","Background",Indicator" so the top row will set the "Normal Text" to the 14th item in the combo for Foreground, the 12th for Background and the 1st for the Indicator.

Why I said it's a pretty crude solution: * It uses SendKeys. No further explanation needed there I'm sure :) * The user has to click the menu/toolbar option in order for it to take effect. * The code isn't best structured (in my opinion) but was based on the amount of time I could dedicate to it at the time. I aim to improve it in the future, but it works fine for me in the current state (so I'll probably leave it!)

Perhaps with a basis, someone can expand on it further.

2

Here's the trick how to keep your color selection permanent. A bit more hex editing. If you made replacement suggested by Bond you should have the following at your hand: RGBA values for basic (classic) 16 colors indexed from 0 to 15, and RGBA values for your custom colors indexed the same way. Now search VBEx.DLL for bytes sequence built from "classic" RGBA values ordered in the following sequence of indexes: 15, 7, 8, 0, 12, 4, 14, 6, 10, 2, 11, 3, 9, 1, 13, 5 (i.e. white, grey, dark grey, black, red, dark red, etc.). The pattern not random, it's derived from values, stored in registry, see Tex Hex's post. The hex string for search should look like 'FFFFFF00C0C0C0008080800000000000FF000000...', etc. 64 bytes total. Replace found bytes sequence with RGBA values of "custom" colors ordered the same way, e.g. 15, 7, 8, etc. Now all colors you choose in VS UI will be stored in the registry and applied after application restart. Well, all, but "indicator" colors. These always reset to defaults. Notes:

  1. In VBE7.DLL (MSO2010) I have found only one sequence of bytes ordered in such way at offset 0x15A98C.
  2. As mentioned in Bond's post there're several bytes sequences for first replacement (basic RGBA values ordered straight from 0 to 15). I have changed only one, the nearest to the offset from previous note (found at 0x15A5AC).
  3. All above you're doing at your own risk :) And don't forget to backup.

Thanks to Tex Hex and Bond for initial research.

UPD: Tested with MSO2007 (VBE6.DLL) and MSO2013 (VBE7.DLL). Work like a charm.

2

Bond's solution (editing the color table in VB6.exe) works perfectly for modifying the color table in the VB6 IDE. However, I found that when a modified color is selected and VB6 is shut down, VB6 is not able to properly save the modified color to the registry.

Normally, VB6 will write an index into the 16-value color table for each text item that can have its color modified.

The color indices are written to the registry here:

HKEY_CURRENT_USER\Software\Microsoft\VBA\Microsoft Visual Basic\CodeForeColors
HKEY_CURRENT_USER\Software\Microsoft\VBA\Microsoft Visual Basic\CodeBackColors

For example, the CodeForeColors value will look something like this:

6 14 5 0 1 3 13 4 15 0 0 0 0 0 0 0

Values from 1 to 16 represent a color in the color table and 0 means 'Auto'.

However, when a modified color is selected, VB6 will write a number outside of the index range to the registry and the CodeForeColors value will look something like this:

5245006 14 5 0 1 3 13 4 15 0 0 0 0 0 0 0

The next time VB6 is started it will not be able to set the correct color for the item that had been set to the modified color.

I'm still trying to figure out a solution but wanted to post what I've found so far.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .