none
Set PowerPoint chart range RRS feed

  • Question

  • What's the right way to specify the chart range when I dynamically add a chart?  This is what I'm trying now

    var chart = shapes.AddChart(XlChartType.xlLine, 66, 113, 828, 384).Chart;
    
    var chartData = chart.ChartData;
    var workbook = chartData.Workbook as Excel.Workbook;
    var worksheet = workbook.Worksheets[1] as Excel.Worksheet;
    
    var row = 0;
    var col = 0;
    
    // Assign data, modifying row/col
    
    var colName = NumberToLetter(col);
    var range = string.Format("Sheet1!A1:{0}{1}", colName, row);
    chart.SetSourceData(range);

    When I look at the chart I'm missing all the series labels from column A.  If I right-click on the chart and select Edit Data, column A is filled in with the proper data, but doesn't have the normal coloring like it would if I created the chart via the UI directly.  So my series are all just an incrementing integer number.

    Monday, April 24, 2017 8:50 PM

All replies

  • Hi Scott H Grosch,

    please refer code below.

    
    

    Add reference to below.

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

    Code:

     private void Form1_Load(object sender, EventArgs e)
            {
             
                Microsoft.Office.Interop.PowerPoint.Application objApp;
                Presentations objPresSet;
                _Presentation objPres;
                Slides objSlides;
                _Slide objSlide;
                TextRange objTextRng;
                Microsoft.Office.Interop.PowerPoint.Shapes objShapes;
                Microsoft.Office.Interop.PowerPoint.Shape objShape;
                SlideShowWindows objSSWs;
                SlideShowTransition objSST;
                SlideShowSettings objSSS;
                SlideRange objSldRng;
                Microsoft.Office.Interop.Graph.Chart objChart;
                Microsoft.Office.Interop.Graph.DataSheet dataSheet;
                objApp = new Microsoft.Office.Interop.PowerPoint.Application();
                objApp.Visible = MsoTriState.msoTrue;
                objPresSet = objApp.Presentations;
                objPres = objPresSet.Open(@"C:\Users\v-padee\Desktop\chart.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
                objSlides = objPres.Slides;
                objSlide = objSlides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
                objTextRng.Text = "Demo Chart";
                objTextRng.Font.Name = "Arial";
                objTextRng.Font.Size = 48;
    
                objChart = (Microsoft.Office.Interop.Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "", MsoTriState.msoFalse).OLEFormat.Object;
                dataSheet = objChart.Application.DataSheet;
               
                dataSheet.Cells[1, 2] = "1"; 
                dataSheet.Cells[1, 3] = "2"; 
                dataSheet.Cells[1, 4] = "3"; 
                dataSheet.Cells[1, 5] = "4"; 
                dataSheet.Cells[1, 6] = "5"; 
                dataSheet.Cells[2, 1] = "Blue"; 
                dataSheet.Cells[2, 2] = "22";
                dataSheet.Cells[2, 3] = "23";
                dataSheet.Cells[2, 4] = "24";
                dataSheet.Cells[2, 5] = "25";
                dataSheet.Cells[2, 6] = "26";
                dataSheet.Cells[3, 1] = "Red"; 
                dataSheet.Cells[3, 2] = "32";
                dataSheet.Cells[3, 3] = "33";
                dataSheet.Cells[3, 4] = "34";
                dataSheet.Cells[3, 5] = "35";
                dataSheet.Cells[3, 6] = "36";
                dataSheet.Cells[4, 1] = "Orange"; 
                dataSheet.Cells[4, 2] = "42";
                dataSheet.Cells[4, 3] = "43";
                dataSheet.Cells[4, 4] = "44";
                dataSheet.Cells[4, 5] = "45";
                dataSheet.Cells[4, 6] = "46";
                dataSheet.Cells[5, 1] = "Light Blue"; 
                dataSheet.Cells[5, 2] = "52";
                dataSheet.Cells[5, 3] = "53";
                dataSheet.Cells[5, 4] = "54";
                dataSheet.Cells[5, 5] = "55";
                dataSheet.Cells[5, 6] = "56";
                objChart.Application.Update();
              
                objChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom;
                objChart.HasTitle = true;
                objChart.ChartTitle.Text = "Chart";
    
               
                objSlide = objSlides.Add(2, PpSlideLayout.ppLayoutBlank);
                objSlide.FollowMasterBackground = MsoTriState.msoFalse;
                objShapes = objSlide.Shapes;
                objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27, "The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);
                int[] SlideIdx = new int[2];
                for (int i = 0; i < 2; i++) SlideIdx[i] = i + 1;
                objSldRng = objSlides.Range(SlideIdx);
                objSST = objSldRng.SlideShowTransition;
                objSST.AdvanceOnTime = MsoTriState.msoTrue;
                objSST.AdvanceTime = 3;
                objSST.EntryEffect = PpEntryEffect.ppEffectBoxOut;
    
                objPres.SaveAs(@"C:\Users\v-padee\Desktop\chartdemo.pptx", PpSaveAsFileType.ppSaveAsPresentation, MsoTriState.msoTrue);
             
                objPres.Close();
                objApp.Quit();
            }

    Output:

    Modify the code as per your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 27, 2017 7:20 AM
    Moderator