3
votes

In PowerPoint 2013 when I manually insert a new chart into a slide, the chart automatically has the Chart Style "Style 1" applied. This has the effect of setting the font sizes of chart labels to 12.

However, no Chart Styles are applied if I try to add a new chart to PowerPoint 2013 using the following C# code:

var ppt = new Microsoft.Office.Interop.PowerPoint.Application();
var presentation = ppt.Presentations.Add();
var layout = Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutChart;
var slide = presentation.Slides.Add(1, layout);
var shapes = slide.Shapes;
shapes.AddChart();

Font sizes seem to default to 18. Is there any way to create a chart or to set Chart Styles using Interop so that it matches a chart produced manually (i.e. has Chart Styles applied)?

I am aware of the Chart.ChartStyle property as mentioned in C# - Excel 2013 how to change chart style but it only seems to change chart colors and legend placement, not font size. There is no Macro recorder in PowerPoint, so I can't generate VBA code to replicate the action of creating a new chart.

1
Hint: Use the macro recorder in Excel. The chart object models are quite similar between the two. You'll need to adjust the results, but it'll give you a good start.Steve Rindsberg
Thanks for the suggestion. The Excel recorder says I should use the ChartStyle property, which changes chart color and legend placement but doesn't change font size of the labels. Yes it is possible to change the font size of the axes labels, legend and data labels separately but I was hoping that there was a more elegant way that avoided this.Justin
I don't believe there is. We got force and we got brute force. I don't think we got elegant.Steve Rindsberg

1 Answers

1
votes

It seems that calling ClearToMatchStyle() after setting ChartStyle applies the font style changes that I was after:

var ppt = new Microsoft.Office.Interop.PowerPoint.Application();
var presentation = ppt.Presentations.Add();
var layout = Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutChart;
var slide = presentation.Slides.Add(1, layout);
var shapes = slide.Shapes;
var chart_shape = shapes.AddChart();
chart_shape.Chart.ChartStyle = 201;
chart_shape.Chart.ClearToMatchStyle();