7

I created a PowerPoint presentation using C#:

PowerPoint.Application powerpointApplication;
PowerPoint.Presentation pptPresentation;
PowerPoint.Slide Slide;

// Create an instance of PowerPoint.
powerpointApplication = new PowerPoint.ApplicationClass();

// Create a PowerPoint presentation.
pptPresentation = powerpointApplication.Presentations.Add(
Microsoft.Office.Core.MsoTriState.msoTrue);


// Create empty slide
Slide = pptPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);

TextRange objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "Remote sensing calendar 1";
objTextRng.Font.Name = "Comic Sans MS";
objTextRng.Font.Size = 48;
// TODO: change color
// objTextRng.Font.Color 



// Save presentation
pptPresentation.SaveAs( BasePath + "result\\2_example.ppt", 
                       PowerPoint.PpSaveAsFileType.ppSaveAsDefault, 
                       MsoTriState.msoTrue // TODO: что за параметр???
                      );
pptPresentation.Close();

Now, how can I change the font color for objTextRng?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
pesten
  • 71
  • 1
  • 1
  • 2

4 Answers4

7

The following code will set the font color to red:

objTextRng.Font.Color.RGB = Color.Red.ToArgb();

If you want to specify a different color, you can use one of the other pre-defined colors, or specify your own RGB values using the Color.FromArgb method.

Either way, make sure that you call the ToArgb method on the Color object that you use. The RGB property requires that an RGB color value be specified.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    Actually, that sets it to blue, despite the name* of that property PowerPoint's interprets colors in BGR format. The easiest (and least elegant) way to set the font color to red is just to specify the color in hex (reversing the R and B bytes): `range.Font.Color.RGB = 0x0000FF;` -- likewise, blue would be `range.Font.Color.RGB = 0xFF0000;`, etc. (*: it _is_ actually RGB format, but it's big endian, meaning the bytes are stored right to left instead of left to right.) – BrainSlugs83 Aug 25 '16 at 18:20
5

Use this for PPTX 2007

    private int BGR(Color color)
    {
        // PowerPoint's color codes seem to be reversed (i.e., BGR) not RGB
        //      0x0000FF    produces RED not BLUE
        //      0xFF0000    produces BLUE not RED
        // so we have to produce the color "in reverse"

        int iColor = color.R + 0xFF * color.G + 0xFFFF * color.B;

        return iColor;
    }

for example

    shape.TextFrame.TextRange.Font.Color.RGB = BGR(Color.Red);  
VVX
  • 51
  • 1
  • 2
  • 2
    This seems to be still the case in Powerpoint 2013 (I guess it's the same format). Kinda silly that when you set `Color.RGB` to red you get blue without this function. :) – Thomas Glaser Apr 01 '16 at 11:55
0

objTextRng.Font.Color.RGB = System.Drawing.ColorTranslator.ToOl(System.Drawing.Color.Blue);

mxrk0
  • 1
0

I think this MSDN page explain it.

EDIT: But this only explain how to do it in VBScript. You can see that the TextRange object have a property Font. This returns the Font object describe here These resources show you that you have access to a RGB property. You can set it like Cody told you. If you need further info, refer to the MSDN section I just point you.

Philippe Lavoie
  • 2,583
  • 5
  • 25
  • 39
  • You can certainly *glean* the explanation from that page, but it's talking about a `DropCap` rather than a `TextRange` object, and the sample code is presented in VB 6.0/VBScript, which is not easily translatable to C#. In particular, there is no `RGB` function in C#. – Cody Gray - on strike Mar 09 '11 at 14:39
  • I agree, I just didn't want to put only the link. It's sometimes annoying, so I just copied the code example. – Philippe Lavoie Mar 09 '11 at 14:41
  • Again though, the problem is that the code sample **will not work in C#**, as the question is tagged. There's no `RGB` function, you have to do it like my answer suggests (which is the preferred way) or import the `Microsoft.VisualBasic` namespace to use the `Information.RGB` function. – Cody Gray - on strike Mar 09 '11 at 14:43
  • @you guys: that link is for Publisher 2010. Not the same as PowerPoint. – Todd Main Mar 09 '11 at 16:26