How to add chart in powerpoint 2007 with C#
<p><strong>I want to add a chart in a slide in a powerpoint2007 file.</strong></p>

<p><strong>Here is my code:</strong></p>

<pre><code>        using PowerPoint = Microsoft.Office.Interop.PowerPoint;
        using Graph = Microsoft.Office.Interop.Graph;
        using Microsoft.Office.Core;


        String sourcePPTPath, targetPPTPath;
        sourcePPTPath = @"e:\test.pptx";
        targetPPTPath = @"e:\copy.pptx";
        PowerPoint.Application pptApp = new PowerPoint.Application();
        PowerPoint.Presentations pptDocSet = pptApp.Presentations;
        PowerPoint._Presentation pptDoc = pptDocSet.Open(sourcePPTPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        PowerPoint.Slides slides = pptDoc.Slides;


        string name = "Pie1";       //The name of the chart
        PowerPoint.Slide slide = slides[1];

        float left, top, width, height;

        foreach (PowerPoint.Shape shape in slide.Shapes)
        {
            if (shape.HasChart == MsoTriState.msoTrue &amp;&amp; shape.Name == name)
            {
                left = shape.Left;
                top = shape.Top;
                width = shape.Width;
                height = shape.Height;
                shape.Delete();

                 //  There is a exception ,how to solve it ?
                slide.Shapes.AddChart(XlChartType.xl3DPie, left, top, width, height);                   
            }
        }

        pptDoc.SaveCopyAs(targetPPTPath, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoFalse);
        pptDoc.Close();
        Marshal.ReleaseComObject(pptDoc);
        pptDoc = null;
        pptApp.Quit();
        Marshal.ReleaseComObject(pptApp);
        pptApp = null;
</code></pre>

<p><strong>The exception is like this:</strong></p>

<h2>Server Error in '/' Application.</h2>

<p>Error HRESULT E_FAIL has been returned from a call to a COM component. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>

<p>Exception Details: System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.</p>

<p>Source Error: </p>

<p>Line 47:                     shape.Delete();
Line 48: 
Line 49:                     slide.Shapes.AddChart(XlChartType.xl3DPie, left, top, width, height);<br>
Line 50:                 }
Line 51:             }</p>

<p>Source File: E:\vscode\pptchart\pptchart\chart.aspx.cs    Line: 49 </p>

<p>Stack Trace: </p>

<p>[COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.]
   Microsoft.Office.Interop.PowerPoint.Shapes.AddChart(XlChartType Type, Single Left, Single Top, Single Width, Single Height) +0
   pptchart.chart.Button1_Click(Object sender, EventArgs e) in E:\vscode\pptchart\pptchart\chart.aspx.cs:49
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563</p>

<hr>

<p>Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 </p>
