How to access palette color in Excel workbook by its index in .NET

Hi,

I’m doing the following to modify my workbook palette :

Dim c As Color = ColorTranslator.FromHtml("#339966")
wb.ChangePalette(c, 1)


Then to check if the palette was changed, I check the value of wb.Colors(1) , is it the right way to do it ?
Because the value doesn’t seem to be changed …

Thanks

Hi,

Well, I tested you scenario and the color palette is changed, following is my test code that works fine, the color is changed and I can get the new color at run time. Also, i have attached the generated workbook, you may open the file into MS Excel 2003 and check the color palette (clicking on the font or fill color icons, the white color is changed to see green color.

Sample code:

Dim wb As New Workbook()

Dim colors() As Color = wb.Colors

MessageBox.Show(colors(1).ToString())

Dim c As Color = ColorTranslator.FromHtml("#339966")

wb.ChangePalette(c, 1)

MessageBox.Show(colors(1).ToString())

wb.Save("f:\test\changepalettebook.xls")

And I have also attached the latest version/fix for you, so kindly try it and let us know if it works fine now.

Thank you.

Thanks for helping

I’ve noticed that before I add the colour to the palette, it is already contained in the palette, at position 49.

Does this mean I can’t add it again at position 0 ? It seems to be the reason anyway…

Thanks!

Hi,

Well, I think you may use Workbook.IsColorInPalette() method to check whether a color is already there in the MS Color Palette.

Thank you.

Fine, I could do that. But what I want is to insert the new colour at a given index, regardless it is in the palette already or not. Is it not possible to have the same colour twice in the palette ? As it stands, it seems to be the case…

Hi,

Well, it is possible, you can have two similar colors on different indexed position on the workbook color palette, see my attached excel file in one of my previous post, the Sea Green color is indexed twice in the color palette.

I am using the latest version/fix v4.7.1.19. By the way, could you not have one color on different indexed positions in the palette using the component? Which version of the component you are using?

Thank you.

Hi,

Yes, using the newest version of the dll attached in your message it works now. The custom colour is added to the palette and it is at two different indices, as expected :slight_smile:

Thanks