Get RGB value of control at runtime

Theres a lot of code out there for getting the windows colour picker dialog, but it can be hard to get a RGB code out of this. For example, if you want to set the colour of a form text box, then get it's RGB value to colour a worksheet cell. (That's what i used it for anyway!)

Heres one method:

VBA:
  1. Private Declare Function GetSysColor Lib "user32" _
  2. (ByVal nIndex As Long) As Long
  3. <blockquote>Function GetRGBColors(ByVal oColor As OLE_COLOR)
  4. If oColor And &H80000000 Then
  5. oColor = GetSysColor(oColor And &HFF&)
  6. End If
  7. GetRGBColors = Array( _
  8. (oColor And &HFF&), _
  9. (oColor And &HFF00&) \ &H100, _
  10. (oColor And &HFF0000) \ &H10000)
  11. End Function
  12.  
  13. Sub RGB()
  14. Dim vRGB() As Variant
  15. vRGB = (GetRGBColors(ActiveCell.Interior.Color))
  16. MsgBox "Red " & vRGB(0) &amp; ",  Green " _
  17. & vRGB(1) & ",  Blue " & vRGB(2)
  18. End Sub

Related posts:

  1. Find First Lower Case Letter
  2. Go Sub, to go?
  3. M.I.E Colour Pallet

Submit a Comment