Changing the range of a Chart in Powerpoint

1.2k views Asked by At

I am experimenting with creating Powerpoint slides out of C#, because I have to create reports out of a tool of mine. The idea to export them to Powerpoint is a given requirement, so I can't change that.

I create and fill a new chart like this:

var shape = objSlide.Shapes.AddChart(XlChartType.xlColumnStacked, 200, 200, 300, 300);
myChart = shape.Chart;

Powerpoint.ChartData pChartData = myChart.ChartData;
Excel.Workbook eWorkbook = (Excel.Workbook)pChartData.Workbook;
Excel.Worksheet eWorksheet = (Excel.Worksheet)eWorkbook.Worksheets[1];

eWorksheet.Cells[2, 1] = "DATA";
//some more Stuff that is not interesting

Now the problem is, that I only need one category, but the default chart creates 3. So I have 3 columns, but I only fill 1 of them. Overwriting them does not work.

Is there a way to change the range to use (like I do in PPT/EXCEL as well by dragging the blue box)?

1

There are 1 answers

0
Xlaech On

Note: if someone finds a better solution, please answer :)

I found one (ugly) way to do it:
https://msdn.microsoft.com/en-us/library/office/ff746759.aspx

Unfortunally, the Powerpoint variation of this Method does not use Range objects, but Excel-Selection Strings. So if i would use this Methode i would have to write a Parser which converts:

worksheet.Range["A1", "B5"]

to

"'Tabelle1'!$A$1:$B$5"

In the end my Solution would look like:

myChart.SetSourceData("'Tabelle1'!$A$1:$B$5", Excel.XlRowCol.xlColumns);