Log in

View Full Version : RGB values from COlor Themes via VBA



YorickG
05-20-2017, 07:58 AM
Hello, It's my first post and I'm completely new to VBA. I'm trying to copy Accent RGB values into textbox on slide in PowerPoint



Shape.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent1
Shape.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
Shape.TextFrame.TextRange.Characters.Text = msoThemeColorAccent1


It fills shape color with Accent 1, but as I supposed returns only position of Accent 1 which is 5... is there a way to return RGB values.. so the result would be sth like A1: 123 144 156

Thank you for your time and help.

Paul_Hossler
05-21-2017, 08:43 AM
Try this




Option Explicit

Sub test()

Dim N As Long, R As Long, G As Long, B As Long

With ActivePresentation.Slides(1).Shapes(1)

.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
N = .Fill.ForeColor.RGB

B = N \ 65536
G = (N - B * 65536) \ 256
R = N - B * 65536 - G * 256

.TextFrame.TextRange.Characters.Text = "R:" & R & " G:" & G & " B:" & B

End With
End Sub

YorickG
05-21-2017, 10:19 PM
Thank you very much :) Now it works! Great job

gmkrishna
01-25-2018, 06:47 AM
This will work fine with standard colors and RGB colors but if theme color used means it will not work. How to find the RGB value for theme colors

John Wilson
01-25-2018, 09:19 AM
It will work for theme colours so maybe explain exactly what you mean.

gmkrishna
01-26-2018, 02:26 AM
This works fine if the text is one of the standard colors OR even a custom color from the RGB color-picker, but it will return nagative long value -738148353 if the text color is one of those in the "theme colors" in Font Color dialog. If you use standard color you can get long value and convert into RGB but if theme color used then you will get negative long value and so you can't able to convert into RGB. It will give wrong RGB value If you try to convert those negative long value.

Please refere my attachement.

21458

John Wilson
01-26-2018, 07:40 AM
I cannot read the code you posted for sure but it looks like WORD not PowerPoint and it looks incorrect

The correct code would be ?Selection.Font.Fill.Forecolor.RGB because in recent versions the selection is TextRange2

gmkrishna
01-29-2018, 02:29 AM
Yes, WORD but it not a big deal. Color always colors where ever we use. My point is if you use theme colors then you can't get the correct RGB value. Because your will get negative value only so, you cant convert those values with your code to get the RGB.

John Wilson
01-29-2018, 02:59 AM
My point is you code is not correct hence the negative value for later versions of word. Use the code I provided above and you should get the correct value.