OLE: Losing Chart Data

P

Paul Brinson

Hi,

When I use Delphi to create a chart in powerpoint and set up the data
that goes into the chart everything seems to look OK when I open the
generated powerpoint file. However, when I double click the chart in
powerpoint the chart's data ends up getting lost and I just get the
sample data for a chart. My formatting is also lost. The chart just
looks like it was just added to the slide. I assume that there is
some magical call that I am missing in my OLE code. I have attached
my sample code.

CREATE POWERPOINT:

m_PowerPoint := TPowerPointApplication.Create(NIL);
m_PowerPoint.Connect();

OPEN PRESENTATION:

m_Presentation := TPowerPointPresentation.Create(nil);
m_Presentation.ConnectTo(m_PowerPoint.Presentations.Open(Filename,
OleVariant(msoFalse), OleVariant(msoTrue), OleVariant(msoFalse)));

CREATE SLIDE: (a copy of a slide in the presentation that was opened)

m_Presentation.Slides.Item(1).Copy;
Slide.ConnectTo(m_Presentation.Slides.Item(m_Presentation.Slides.Count));

CREATE CHART AND SET DATA:

dataShape := Slide.Shapes.AddOLEObject(topX, topY, width, height,
'MSGraph.Chart', '', msoFalse, '', -1, '', msoFalse);

chartObj := dataShape.OLEFormat;

chart := IDispatch(chartObj.Object) as Graph_TLB.Chart;
chart.ChartType := xlBarClustered;
chart.HasDataTable := False;
chart.Legend.Position := xlLegendPositionBottom;
Axis(chart.Axes(xlCategory, xlPrimary)).HasMajorGridlines := False;
Axis(chart.Axes(xlCategory, xlPrimary)).HasMinorGridlines := False;
Axis(chart.Axes(xlValue, xlPrimary)).HasMajorGridlines := False;
Axis(chart.Axes(xlValue, xlPrimary)).HasMinorGridlines := False;

chartData := chart.Application.DataSheet;

chartData.Rows.ClearContents();
chartData.Columns.ClearContents();

chartData.Cells.Item[1, 2] := 'Series 1';
chartData.Cells.Item[1, 3] := 'Series 2';
chartData.Cells.Item[1, 4] := 'Series 3';
chartData.Cells.Item[2, 1] := 'Target';
chartData.Cells.Item[2, 2] := '56';
chartData.Cells.Item[2, 3] := '65';
chartData.Cells.Item[2, 4] := '76';
chartData.Cells.Item[3, 1] := 'Overall';
chartData.Cells.Item[3, 2] := '67';
chartData.Cells.Item[3, 3] := '76';
chartData.Cells.Item[3, 4] := '75';

I thought that running the dataShape.OLEFormat.DoVerb(1) to execute
the edit command on the OLE object would work, but that throws an
exception complaining about the presentation not being in slide view.
Unfortunately I don't appear to have a window in the powerpoint
application object to change the view type for. Was the whole
DoVerb() thing a wild goose chase?

Thanks!

~Paul
 
S

Steve Rindsberg

You need to call the chart (ie, MSGraph) application's .Update method before
calling its .Quit method. I think calling chart.Update should do it in your
case.

Paul Brinson said:
Hi,

When I use Delphi to create a chart in powerpoint and set up the data
that goes into the chart everything seems to look OK when I open the
generated powerpoint file. However, when I double click the chart in
powerpoint the chart's data ends up getting lost and I just get the
sample data for a chart. My formatting is also lost. The chart just
looks like it was just added to the slide. I assume that there is
some magical call that I am missing in my OLE code. I have attached
my sample code.

CREATE POWERPOINT:

m_PowerPoint := TPowerPointApplication.Create(NIL);
m_PowerPoint.Connect();

OPEN PRESENTATION:

m_Presentation := TPowerPointPresentation.Create(nil);
m_Presentation.ConnectTo(m_PowerPoint.Presentations.Open(Filename,
OleVariant(msoFalse), OleVariant(msoTrue), OleVariant(msoFalse)));

CREATE SLIDE: (a copy of a slide in the presentation that was opened)

m_Presentation.Slides.Item(1).Copy;
Slide.ConnectTo(m_Presentation.Slides.Item(m_Presentation.Slides.Count));

CREATE CHART AND SET DATA:

dataShape := Slide.Shapes.AddOLEObject(topX, topY, width, height,
'MSGraph.Chart', '', msoFalse, '', -1, '', msoFalse);

chartObj := dataShape.OLEFormat;

chart := IDispatch(chartObj.Object) as Graph_TLB.Chart;
chart.ChartType := xlBarClustered;
chart.HasDataTable := False;
chart.Legend.Position := xlLegendPositionBottom;
Axis(chart.Axes(xlCategory, xlPrimary)).HasMajorGridlines := False;
Axis(chart.Axes(xlCategory, xlPrimary)).HasMinorGridlines := False;
Axis(chart.Axes(xlValue, xlPrimary)).HasMajorGridlines := False;
Axis(chart.Axes(xlValue, xlPrimary)).HasMinorGridlines := False;

chartData := chart.Application.DataSheet;

chartData.Rows.ClearContents();
chartData.Columns.ClearContents();

chartData.Cells.Item[1, 2] := 'Series 1';
chartData.Cells.Item[1, 3] := 'Series 2';
chartData.Cells.Item[1, 4] := 'Series 3';
chartData.Cells.Item[2, 1] := 'Target';
chartData.Cells.Item[2, 2] := '56';
chartData.Cells.Item[2, 3] := '65';
chartData.Cells.Item[2, 4] := '76';
chartData.Cells.Item[3, 1] := 'Overall';
chartData.Cells.Item[3, 2] := '67';
chartData.Cells.Item[3, 3] := '76';
chartData.Cells.Item[3, 4] := '75';

I thought that running the dataShape.OLEFormat.DoVerb(1) to execute
the edit command on the OLE object would work, but that throws an
exception complaining about the presentation not being in slide view.
Unfortunately I don't appear to have a window in the powerpoint
application object to change the view type for. Was the whole
DoVerb() thing a wild goose chase?

Thanks!

~Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top