c# Excel Interop known Color as RGB does not display right in shape in sheet

ptuga 21 Reputation points
2021-11-10T20:05:57.463+00:00

Hello,

I'm trying to create a textbox and fill it with a specific colour.
So far, I managed to do it but the result seems not to match with the colour that I specify.
This is an example:

Color colour = Color.Red; // -> define my colour
Excel.Shape textbox = worksheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, (float)x3, (float)y3, 100, 100);
var infoToDisplay = "#####";
textbox.TextFrame.Characters(System.Type.Missing, System.Type.Missing).Text = infoToDisplay;
textbox.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = Color.White.ToArgb(); //-> text in white
textbox.Fill.ForeColor.RGB = colour.ToArgb(); // -> fill colour using a known Color

The result is a Blue fill not Red as I am specifying.
This is the same for different types of shapes e.g. line.

What is happening?
This may be a simple correction but I'm not sure why it happens.
Can anyone support, please?

Thank you.

Regards,
Pedro

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,721 Reputation points
    2021-11-10T20:29:32.83+00:00

    Use

     textbox.Fill.ForeColor.RGB =  ColorTranslator.ToWin32(colour);
    

0 additional answers

Sort by: Most helpful