Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Dear Expert,

I was making some automation in PowerPoint 2010. Where I want to automate some charts using c# .net referring Microsoft powerpoint interop assemblies.
I got success to some extent but as I moved to access chart's properties, its just becoming unreachable.

C#
public void createMyChart()
{
  //Instantiate slide object
                Microsoft.Office.Interop.PowerPoint.Slide objSlide = null;

                //Access the first slide of presentation
                objSlide = objPres.Slides[1];

                //Select firs slide and set its layout
                objSlide.Select();
                objSlide.Layout = Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank;

                //Add a default chart in slide
                //pptNS.Shape objShape = objSlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xl3DColumn, 20F, 30F, 400F, 300F);
                objSlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xlLine, 20F, 30F, 400F, 300F);
                
                

                objSlide.Shapes[1].Chart.Select();  


                //Access the added chart
                Microsoft.Office.Interop.PowerPoint.Chart objChart = null;
                
                objChart = objSlide.Shapes[1].Chart;
                //objChart.HasTitle = true;
                //objChart.ChartTitle.Caption = "US Chart";
                
                
                 
               
               

                //Access the chart data
                Microsoft.Office.Interop.PowerPoint.ChartData chartData = objChart.ChartData;


                //Create instance to Excel workbook to work with chart data
                Microsoft.Office.Interop.Excel.Workbook dataWorkbook = (Microsoft.Office.Interop.Excel.Workbook)chartData.Workbook;

                //Accessing the data worksheet for chart
                Microsoft.Office.Interop.Excel.Worksheet dataSheet = dataWorkbook.Worksheets[1];


        


 




              

                



                //Applying the set range on chart data table
                

                Microsoft.Office.Interop.Excel.ListObject tbl1 = dataSheet.ListObjects["Table1"];

                xlNS.Range testRange = dataSheet.get_Range("A1", Type.Missing);
                xlNS.Range tr1 = testRange.get_End(xlNS.XlDirection.xlDown);
                xlNS.Range tr2 = tr1.get_End(xlNS.XlDirection.xlDown);

                xlNS.Range dateRange = dataSheet.Cells.get_Range("A1", tr2);


                dateRange.EntireColumn.Cells.NumberFormat = "m/dd/yyyy";

                xlNS.Range rg1 = dataSheet.get_Range("B1", Type.Missing);
                xlNS.Range rg2 = rg1.get_End(xlNS.XlDirection.xlDown);
                xlNS.Range rg3 = rg2.get_End(xlNS.XlDirection.xlDown);
                xlNS.Range finalRange = dataSheet.Cells.get_Range("A1", rg3);

             
                tbl1.Resize(finalRange);

   





                //Setting chart title
                objChart.ChartTitle.Font.Italic = true;
                objChart.ChartTitle.Text = "2007 Sales";
                objChart.ChartTitle.Font.Size = 18;
                objChart.SeriesCollection("M1% YoY Index");

                //objChart.ChartTitle.Font.Color = "Dark Red"; 
                objChart.ChartTitle.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                objChart.PlotArea.Select();
                objChart.HasLegend = true;
                objChart.Legend.Position = pptNS.XlLegendPosition.xlLegendPositionBottom;
                
                //objChart.ChartTitle.Format.Line.ForeColor.RGB = Color.Black.ToArgb();

                //Accessing Chart value Y-axis
                
                Microsoft.Office.Interop.PowerPoint.Axis valaxis = objChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);
                valaxis.HasTitle = true;
                
                //Setting values axis units
                valaxis.MajorUnit = 6.8;
                valaxis.MinorUnitIsAuto = true;
                valaxis.MinimumScaleIsAuto = true;
                valaxis.MaximumScaleIsAuto = true;
                
                valaxis.MajorGridlines.Format.Line.ForeColor.RGB = 0;
                valaxis.Format.Line.ForeColor.RGB = 0;
                valaxis.TickLabels.Font.Size = 12;
                


                //Accessing Chart Value Y-Axis

                int MajorUnit_X = 0;
                MajorUnit_X = MajorUnitX_Axis.minDivValue(93);


                pptNS.Axis xAxis = objChart.Axes(pptNS.XlAxisType.xlCategory, pptNS.XlAxisGroup.xlPrimary);

                xAxis.MinorUnitIsAuto = true;
                xAxis.MinimumScaleIsAuto = true;
                xAxis.MaximumScaleIsAuto = true;
                xAxis.MajorUnit = 12;
                xAxis.Format.Line.ForeColor.RGB = 0;


                xAxis.TickLabels.Font.Size = 12;
                xAxis.TickLabels.Font.FontStyle = "Regular";
                xAxis.TickLabelSpacingIsAuto = true;
                xAxis.TickLabels.NumberFormat = "[$-409]mmm-yy;@";
                int tempValue = xAxis.TickLabels.Alignment;
               
                pptNS.XlTickLabelOrientation obj = pptNS.XlTickLabelOrientation.xlTickLabelOrientationDownward;

               
        
                //objChart.RightAngleAxes = false;

                // Save the presentation as a PPTX
                objPres.SaveAs("C:\\VSTOSampleChart", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Microsoft.Office.Core.MsoTriState.msoTrue);
            

                //Close Workbook and presentation
                dataWorkbook.Application.Quit();
                objPres.Application.Quit();
                objPPT.Quit();
}



I am using VSTO. I want to change Line color of line chart to Dark Red which is by default Blue. Usually we go to Format Data series menu for the same. I want it to do the same using c# interop.


Thanks and regards.
Posted

1 solution

You can do this to change line color,

foreach (var tmp in objChart.SeriesCollection())
{
//Check for the series that you want to change color
if (tmp.Name == "Series1")
{
tmp.Border.Color = Color.Green;
}
}
 
Share this answer
 

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