I can create PowerPoint tables with many cells, but the default font (28 points) is too large. Without access to the user’s PowerPoint template, how can I set the font for a table I have created? All I can do at the moment is:
- Create the table.
- Fill every cell with a single space because otherwise font changes are ignored
(though it works using the PowerPoint UI). -
Create a range containing all cells and set font:
List<string> names = new List<string>(); foreach (PowerPoint.Column column in table.Columns) foreach (PowerPoint.Cell cell in column.Cells) { cell.Shape.TextFrame.TextRange.Text = ""; names.Add(cell.Shape.Name); } PowerPoint.ShapeRange range = ppt.ActivePresentation.Slides.Item(1).Shapes.Range(names.ToArray()); range.TextFrame.TextRange.Font.Size = 12;
I am using C# to control PowerPoint 2003..2010 via its COM API. My experiments have been with PowerPoint 2003.
In PPT 2010, this works w/o having to enter text into the cells (vba, but should be easy enough to translate). For demo purposes, I’m working with the currently selected table; substitute any other reference to the table as oTbl:
In PPT2003, as you’ve seen, you need to add text to the cell for the formatting to “take” (which seriously slows things down, unfortunately).
Because there are other instances in PPT automation where you may need to temporarily fill in text then delete it, you might want to write a more general routine that you can pass a shape to.
“Pre-treat” the shape with this, do whatever’s necessary, then “Un-treat” it with a companion routine that tests to see if the text = “!@#$%” and sets it back to “” if so, leaves it alone otherwise.