Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project which can create a new powerpoint through c# window application.
I meet some problems during insert a chart into a slide.
when you want to insert a chart into a slide, there is a default data source for you, I already realize change the data in the excel, but I can't change the data range which really bothers me.

the code is :
slide = pptApp.Presentations[presentationCount].Slides[slideCount];
            shapeCount = slide.Shapes.Count;
            PPT.Shape shape;
            shape = slide.Shapes.AddChart2(typenum, type, left, top, width, height,false);
            PPT.Chart chart = slide.Shapes[shapeCount + 1].Chart;
             
            object missing = System.Reflection.Missing.Value;

            //input data
            chart.ChartData.Activate();
            int num = kinds.Count();
            Excel.Workbook workbook = chart.ChartData.Workbook;
            Excel.Worksheet sheet = chart.ChartData.Workbook.Worksheets["Sheet1"];
            sheet.Cells.Clear();
            
            Excel.Range range;
            object[] objHeaders = { "数量", "数据1" };
            range = sheet.get_Range("A1", "B1");
            range.set_Value(Type.Missing, objHeaders);

            var data = new object[num, 2];
            foreach(int n in Enumerable.Range(0,num))
            {
                data[n, 0] = kinds[n];
                data[n, 1] = values[n];
            }
            
            range = sheet.get_Range("A2", "B" + (num + 1));
            sheet.get_Range("A2", "B" + (num + 1)).Value = data;
            sheet.get_Range("B1").Value = title;
            //Excel.Range chartRange = sheet.get_Range("A1", "B5");
            chart.ChartWizard(sheet.get_Range("A1", "B5"), missing, missing, Microsoft.Office.Interop.Excel.XlRowCol.xlColumns, 1, 1, true, "多糖商品销量分析", "月份", "销量", missing);

            //set different part's color
            for (int i = 1; i < 2; i++)
            {
                PPT.Series series = chart.SeriesCollection(i);
                for (int j = 1; j <=  num; j++)
                {
                    PPT.Point point = series.Points(j);
                    point.Format.Fill.ForeColor.RGB = color[j-1];
                }
            }


I want to use chart.ChartWizard(), but there is always a mistake:
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900