Results 1 to 4 of 4
  1. #1
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Custom Colors in Userforms (Office (Any))

    A perennial annoyance when developing Office userforms is the limited selection of colors that are available for applying to userforms and controls. MS provides the ability to create and use custom colors when working with drawing objects in Word, Excel, PPT etc., but when working with userforms, you're stuck with the 64 'Palette' color presets as well as perhaps a few additional colors available under the 'System' tab. The limited color choices can be a problem if you're working to particular specs, for example trying to make userforms visually compatible with graphic elements in a document.

    Don't know if this has been documented anywhere, but it turns out that you actually can use any custom color at all when working in userforms. The key is to get the custom color's RGB value, and translate that to a hexadecimal value so it can be used to specify the BackColor or ForeColor property of a given userform or control.

    Here's some code that demonstrates translating an RGB value into a hexadecimal value:
    <pre>Public Sub GetHexValueFromRGB()

    Dim lngRGBVal As Long
    Dim strHexVal As String
    Dim strPadHexVal As String

    'Get Long value for color, based on given RGB values:
    lngRGBVal = RGB(255, 247, 225)
    Debug.Print lngRGBVal

    'Get Hexidecimal value for color, based on Long value:
    strHexVal = Hex(lngRGBVal)
    Debug.Print strHexVal

    'Pad result of Hex function, with required prefix and suffix
    strPadHexVal = "&H" & strHexVal & "&"
    Debug.Print strPadHexVal

    End Sub</pre>

    The attached Word document contains details on how to get a custom color's RGB value, as well as how to programatically assign hex color values to userform controls.

    Gary

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Custom Colors in Userforms (Office (Any))

    Interesting stuff. I stashed it away, just in case.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Colors in Userforms (Office (Any))

    You do not really need the hexadecimal value, you can also use the decimal value. For example, type <code>? RGB(230, 255, 230)</code> in the immediate window, and copy the result 15138790 to the clipboard. You can paste this value into the ForeColor or BackColor property of a control, the VBE will convert it to hexadecimal automatically.

    If you assign colors in code, you can use the RGB function directly. In the sample form in your attachment, you might use

    Private Sub UserForm_Initialize()
    Dim Ctrl As MSForms.Control
    For Each Ctrl In Me.Controls
    If TypeName(Ctrl) = "Label" Then
    Ctrl.BackColor = RGB(230, 255, 230)
    End If
    Next Ctrl
    End Sub

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Custom Colors in Userforms (Office (Any))

    Hans,

    That's great - thanks for making it even simpler.
    It's funny how our minds work in habitual boxes - all these years it never occurred to me to try substituting a custom color hex value, let alone simply a numeric one...

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •