12

I have a problem in a C# project. In fact, I created a PowerPoint add-in and I want to generate charts on slides.

I created a slide with:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Graph;

Microsoft.Office.Interop.Graph.Chart objChart;
objChart = (Microsoft.Office.Interop.Graph.Chart)objShape.OLEFormat.Object;`

The chart is created on the slide but I can't access the data to update or insert.

I have tried using a Datasheet like below:

//DataSheet test = objChart.Application.DataSheet;
//test.Cells.Clear()

This deleted the data of the chart but I couldn't figure out how to insert values back into the chart data afterwards.

James Black
  • 41,583
  • 10
  • 86
  • 166
user361369
  • 121
  • 1
  • 3

4 Answers4

2

Ok, so for starters, make sure you include the following references:

  • From the .Net library:
    • Microsoft.Office.Interop.Graph
    • Microsoft.Office.Interop.Powerpoint
  • From the COM library:
    • Microsoft Office XX Object Library (where XX is your organization's most widely used Office version [really doesn't matter if you include it in your package])

Add this to your declarations section:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using Core = Microsoft.Office.Core;

Then, try out this snippet:

PowerPoint.Application app = new PowerPoint.Application();
app.Visible = Core.MsoTriState.msoTrue; // Sure, let's watch the magic as it happens.

PowerPoint.Presentation pres = app.Presentations.Add();
PowerPoint._Slide objSlide = pres.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

PowerPoint.TextRange textRange = objSlide.Shapes[1].TextFrame.TextRange;
textRange.Text = "My Chart";
textRange.Font.Name = "Comic Sans MS";  // Oh yeah I did
textRange.Font.Size = 24;
Graph.Chart objChart = (Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320,
    "MSGraph.Chart.8", "", Core.MsoTriState.msoFalse, "", 0, "", 
    Core.MsoTriState.msoFalse).OLEFormat.Object;

objChart.ChartType = Graph.XlChartType.xl3DPie;
objChart.Legend.Position = Graph.XlLegendPosition.xlLegendPositionBottom;
objChart.HasTitle = true;
objChart.ChartTitle.Text = "Sales for Black Programming & Assoc.";  // I'm a regular comedian.

Should work like a champ. I hope this helps.

Joshua
  • 617
  • 9
  • 23
2

You can also use

PowerPoint.Chart

It is available if you are working on Office 10 or greater.

Here is the code

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

then, declare these object outside your function

PowerPoint.Slide pSlide = null;
PowerPoint.Shape pShape = null;
PowerPoint.Chart pChart = null;

In your function definition

pSlide = this.Application.ActivePresentation.Slides[1];
pShape = slide.Shapes.AddChart(Office.XlChartType.xlColumnStacked, 200, 200, 300, 300);//These values tell where the chart will be positioned
pChart = pShape.Chart;

Now, in order to access the chart data, you have to create an excel workbook and worksheet object.

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

Now you can access the chart data by

(Excel.Range)eWorksheet.Cells.get_Range("A1", missing).get_Value();
Zunair Zubair
  • 233
  • 1
  • 15
0

Have you tried using the OpenXML SDK, I've used it to construct complicated Excel Sheets and Word Documents, I believe it supports Powerpoint as well. See http://www.microsoft.com/en-us/download/details.aspx?id=5124.

AMissico
  • 21,470
  • 7
  • 78
  • 106
Sabry
  • 135
  • 2
  • 9
0

Just to consider...before referencing WorkBook property of an existing chart call the ChartData.Activate Method

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.chartdata.activate%28v=office.14%29.aspx