2

I have written below piece of code to change background color of table cell of ppt using c# code but nothing is happening:

//creating powerpoint aaplication
PowerPoint.Application pptApp = new PowerPoint.Application();
pptApp.Visible = Office.MsoTriState.msoTrue;
var pptPresent = pptApp.Presentations;
var fileOpen = pptPresent.Open(@file, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, Office.MsoTriState.msoTrue);
// getting first slide
PowerPoint.Slide objSlide = fileOpen.Slides[1];
PowerPoint.Shapes item = objSlide.Shapes;
// getting first shape
var shape1 = item[1];
// check if shape is table
if (shape1.HasTable == Office.MsoTriState.msoTrue)
{
    // change the table cell to red color
    shape1.Table.Cell(2, 5).Shape.Fill.BackColor.RGB = System.Drawing.Color.Red.ToArgb();
    // make it visible
    shape1.Fill.Visible = Office.MsoTriState.msoTrue;
}
// saving the ppt
fileOpen.SaveAs(openFolder + subStr + ".pptx",PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Office.MsoTriState.msoTrue);
// close the ppt
fileOpen.Close();

Above piece of code is not working as expected, can someone help me?

talnicolas
  • 13,885
  • 7
  • 36
  • 56
shashank
  • 61
  • 1
  • 5

2 Answers2

2

To change the cell color ForeColor needs to be used instead of BackColor. Drawing colors may not work as expected. To change that use ColorTranslator.

shape1.Table.Cell(2, 5).Shape.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
cadand
  • 21
  • 3
0

Great question! The Fill property is also available for the Shape object, wether it's a Cell or a another type of Shape.

Another example for the Fill property is mentioned in this thread: https://stackoverflow.com/a/26253177/20337158.

Also from the Microsoft learning sites: https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff763049(v=office.12)

opkvitne
  • 1
  • 3
  • Welcome to Stackoverflow. Please do not just post a link as an answer. Add some description to show how the link would help solving the problem. – MD Zand Dec 05 '22 at 07:21