h = actxserver('Excel.Application')
h.Visible = 1
b = h.Workbooks.Add
w = b.Worksheets.Add;
get(w.Range('A1').Interior);
This example is somewhat boring because you're creating the worksheet from scratch, so you know that the color is already, but what you're looking for is the Interior property.
It'll look something like this when you get there:
Interface.Microsoft_Excel_11.0_Object_Library._Workbook
Application: [1x1 Interface.Microsoft_Excel_11.0_Object_Library._Application]
Creator: 'xlCreatorCode'
Parent: [1x1 Interface.Microsoft_Excel_11.0_Object_Library.Range]
Color: 16777215
ColorIndex: -4142
InvertIfNegative: [1x241 char]
Pattern: -4142
PatternColor: 0
PatternColorIndex: -4142
You'll have to look up what that Color field actually means. You can loop over what range you're interested and get the color of each cell.
Cheers,
Robert
"Ben Taylor" <bentay...@gmail.com> wrote in message <hbdd6f$snb$1...@fred.mathworks.com>...
VBA colors are in Microsoft decimal RGB format: 0xRRGGBB. For example, in this specific case, 16777215 = 0xFFFFFF => Matlab color [1.0,1.0,1.0]. Reversing this conversion, a Matlab color of [.2,.4,.8] => 0x3366CC = 3368652
Here's a handy function I have for the Matlab=>VBA color conversion, which also handles string colors. Doing a reverse function is left as an exercise to the reader...
%% Convert Matlab color to a VBA color (Microsoft decimal RGB format: 0xRRGGBB)
function color = m2vbColor(color)
try
% Convert color names to RBG triple (0-1) if not already in that format
if ischar(color)
switch lower(color)
case {'y','yellow'}, color = [1,1,0];
case {'m','magenta'}, color = [1,0,1];
case {'c','cyan'}, color = [0,1,1];
case {'r','red'}, color = [1,0,0];
case {'g','green'}, color = [0,1,0];
case {'b','blue'}, color = [0,0,1];
case {'w','white',''}, color = [1,1,1]; % empty '' also sets white color
case {'k','black'}, color = [0,0,0];
otherwise, error(['Invalid color specified: ' color]);
end
elseif ~isnumeric(color) | length(color)~=3 %#ok ML6
error(['Invalid color specified: ' color]);
end
% Convert to Microsoft decimal RGB format
color = sum(floor(color*255) .* (256.^[0,1,2]));
catch
error(['Invalid color specified: ' lasterr]);
end
Yair Altman
http://UndocumentedMatlab.com
i have a few things to add to you post concerning the VBA representation of colors in the hexadecimal format.
> VBA colors are in Microsoft decimal RGB format: 0xRRGGBB. For example, in this specific case, 16777215 = 0xFFFFFF => Matlab color [1.0,1.0,1.0]. Reversing this conversion, a Matlab color of [.2,.4,.8] => 0x3366CC = 3368652
while the 0xRRGGBB format is true for html rgb format, it is slightly different for VBA code which actually (don't ask me why) uses 0xBBGGRR format. Using the html code would result in a wrong color.
The function you suggested does indeed look very handy but needs altering. The piece where you convert the rgb color to the hex format should now be (at least i think, i'm at home now so i couldn't check):
> % Convert to Microsoft decimal RGB format
> color = sum(floor(color*255) .* (256.^[2,1,0]));
Greets
Sander