none
How to copy excel cell range and chart data to power point slides RRS feed

  • Question

  • i have data and chart in excel work sheet which i need to copy at run time from excel to power point slides.

    i have a code which is working fine but the code only can copy chart data to excel sheet not range data.

    please see the scree shot of my excel. so anyone can get idea how data is there in my work sheet which i need to copy to power point slide programmatically.

    here is code which i am using to copy range data and chart data to power point dynamically.

            private void Form1_Load(object sender, EventArgs e)
            {
                pptNS.ApplicationClass powerpointApplication = null;
                pptNS.Presentation pptPresentation = null;
                pptNS.Slide pptSlide = null;
                pptNS.ShapeRange shapeRange = null;
    
                xlNS.ApplicationClass excelApplication = null;
                xlNS.Workbook excelWorkBook = null;
                xlNS.Worksheet targetSheet = null;
                xlNS.ChartObjects chartObjects = null;
                xlNS.ChartObject existingChartObject = null;
                xlNS.Range destRange = null;
    
                string paramPresentationPath = @"D:\test\Chart Slide.pptx";
                string paramWorkbookPath = @"D:\test\MyExcelData.xlsx";
                object paramMissing = Type.Missing;
    
    
                try
                {
                    // Create an instance of PowerPoint.
                    powerpointApplication = new pptNS.ApplicationClass();
    
                    // Create an instance Excel.          
                    excelApplication = new xlNS.ApplicationClass();
    
                    // Open the Excel workbook containing the worksheet with the chart
                    // data.
                    excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing);
    
                    // Get the worksheet that contains the chart.
                    targetSheet =
                        (xlNS.Worksheet)(excelWorkBook.Worksheets["Spain"]);
    
                    // Get the ChartObjects collection for the sheet.
                    chartObjects =
                        (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));
    
    
    
                    // Create a PowerPoint presentation.
                    pptPresentation = powerpointApplication.Presentations.Add(
                                        Microsoft.Office.Core.MsoTriState.msoTrue);
    
                    // Add a blank slide to the presentation.
                    pptSlide =
                        pptPresentation.Slides.Add(1, pptNS.PpSlideLayout.ppLayoutBlank);
    
                    // capture range
                    //var writeRange = targetSheet.Range["A1:B15"];
                    destRange = targetSheet.get_Range("A1:B15");
                    //copy range
                    destRange.Copy();
    
                    // Paste the chart into the PowerPoint presentation.
                    shapeRange = pptSlide.Shapes.Paste();
    
    
                    // Position the chart on the slide.
                    shapeRange.Left = 60;
                    shapeRange.Top = 100;
    
                    // Get or capture the chart to copy.
                    existingChartObject =(xlNS.ChartObject)(chartObjects.Item(1));
                        
    
                    // Copy the chart from the Excel worksheet to the clipboard.
                    existingChartObject.Copy();
    
                    // Paste the chart into the PowerPoint presentation.
                    shapeRange = pptSlide.Shapes.Paste();
                    //Position the chart on the slide.
                    shapeRange.Left = 90;
                    @shapeRange.Top = 100;
    
                    // Save the presentation.
                    pptPresentation.SaveAs(paramPresentationPath,
                                    pptNS.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
                                    Microsoft.Office.Core.MsoTriState.msoTrue);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    // Release the PowerPoint slide object.
                    shapeRange = null;
                    pptSlide = null;
    
                    // Close and release the Presentation object.
                    if (pptPresentation != null)
                    {
                        pptPresentation.Close();
                        pptPresentation = null;
                    }
    
                    // Quit PowerPoint and release the ApplicationClass object.
                    if (powerpointApplication != null)
                    {
                        powerpointApplication.Quit();
                        powerpointApplication = null;
                    }
    
                    // Release the Excel objects.
                    targetSheet = null;
                    chartObjects = null;
                    existingChartObject = null;
    
                    // Close and release the Excel Workbook object.
                    if (excelWorkBook != null)
                    {
                        excelWorkBook.Close(false, paramMissing, paramMissing);
                        excelWorkBook = null;
                    }
    
                    // Quit Excel and release the ApplicationClass object.
                    if (excelApplication != null)
                    {
                        excelApplication.Quit();
                        excelApplication = null;
                    }
    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
    
                }
            }

    please see my code and tell me what to rectify in my code as a result cell range and chart both i can copy to power point slides.

    thanks


    • Edited by Sudip_inn Tuesday, January 31, 2017 1:15 PM
    Tuesday, January 31, 2017 1:09 PM

Answers

  • Hi Mou_inn,

    As I understand from your description , you want to copy the Excel Range to Powerpoint slide.

    please refer the example below.

    data I have in sheet.

    Code:

     private void Form1_Load(object sender, EventArgs e)
            {
                Excel.Application objExcel = new Excel.Application();
                object missing = Type.Missing;
                Excel.Workbook wk = objExcel.Workbooks.Open(@"C:\Users\v-padee\Desktop\chartdata.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                objExcel.Visible = true;
                Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
                Excel.Range range = ws.Application.get_Range("A1:D4", missing);
                range.Copy(missing);
    
                PowerPoint.Application objPower = new PowerPoint.Application();
                objPower.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                PowerPoint.Presentation presentation = objPower.Presentations.Open(@"C:\Users\v-padee\Desktop\Presentation11.pptm", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
                PowerPoint.Slide slide = presentation.Slides[1];
                slide.Select();
                slide.Design.Application.ActiveWindow.View.Paste();
               
            }

    Output:

    you need to merge this code with your above mentioned code to work together and copy excel chart and excel range to powerpoint slide.

    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.

    Wednesday, February 1, 2017 3:33 AM
    Moderator
  • Hi Mou_inn,

    which mode is active when you run the code?

    it is possible that file is in presentation mode or in print preview mode or is not visible.

    in that case you can get this error.

    I find some issue with your above code.

    to place the data at particular location, you can set top and left property of shape after pasting the data.

    example:

     private void Form1_Load(object sender, EventArgs e)
            {
                Excel.Application objExcel = new Excel.Application();
                object missing = Type.Missing;
                Excel.Workbook wk = objExcel.Workbooks.Open(@"C:\Users\v-padee\Desktop\chartdata.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                objExcel.Visible = true;
                Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
                Excel.Range range = ws.Application.get_Range("A1:D4", missing);
                range.Copy(missing);
    
                PowerPoint.Application objPower = new PowerPoint.Application();
                objPower.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                PowerPoint.Presentation presentation = objPower.Presentations.Open(@"C:\Users\v-padee\Desktop\Presentation11.pptm", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
                PowerPoint.Slide slide = presentation.Slides[1];
                slide.Select();
                slide.Design.Application.ActiveWindow.View.Paste();
                slide.Shapes[1].Left=100;
                slide.Shapes[1].Top = 100;
            }

    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.

    • Marked as answer by Sudip_inn Friday, February 3, 2017 1:15 PM
    Thursday, February 2, 2017 8:26 AM
    Moderator

All replies

  • Hi Mou_inn,

    As I understand from your description , you want to copy the Excel Range to Powerpoint slide.

    please refer the example below.

    data I have in sheet.

    Code:

     private void Form1_Load(object sender, EventArgs e)
            {
                Excel.Application objExcel = new Excel.Application();
                object missing = Type.Missing;
                Excel.Workbook wk = objExcel.Workbooks.Open(@"C:\Users\v-padee\Desktop\chartdata.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                objExcel.Visible = true;
                Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
                Excel.Range range = ws.Application.get_Range("A1:D4", missing);
                range.Copy(missing);
    
                PowerPoint.Application objPower = new PowerPoint.Application();
                objPower.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                PowerPoint.Presentation presentation = objPower.Presentations.Open(@"C:\Users\v-padee\Desktop\Presentation11.pptm", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
                PowerPoint.Slide slide = presentation.Slides[1];
                slide.Select();
                slide.Design.Application.ActiveWindow.View.Paste();
               
            }

    Output:

    you need to merge this code with your above mentioned code to work together and copy excel chart and excel range to powerpoint slide.

    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.

    Wednesday, February 1, 2017 3:33 AM
    Moderator
  • when i am pasting excel cell range in power point then how could i position it in ppt slide.

    slide.Select();
    slide.Design.Application.ActiveWindow.View.Paste();

    when pasting chart in ppt file then we could re-position it by top and left property. so tell me how could i re-position cell range ?

    thanks

    Wednesday, February 1, 2017 8:33 AM
  • one request that i am curious to know how could i create line chart in ppt slide at runtime. so i post a question. please answer it if possible. https://social.msdn.microsoft.com/Forums/vstudio/en-US/b6f4f621-9ef5-431a-86b5-5fe6b374dd90/c-how-to-create-line-char-directly-into-power-point-slide-from-my-data-table-data?forum=exceldev

    have a look please. thanks

    Wednesday, February 1, 2017 9:12 AM
  • the moment i try to incorporate your code then getting error. error message is

    Slide (unknown member) : Invalid request.  This view does not support selection.

    this is the code

                    destRange = targetSheet.get_Range("A1:B15");
                    pptSlide.Select();
                    pptSlide.Design.Application.ActiveWindow.View.Paste();
    

    when this line execute pptSlide.Select(); then got the above error message.

    my full code as follows

                pptNS.ApplicationClass powerpointApplication = null;
                pptNS.Presentation pptPresentation = null;
                pptNS.Slide pptSlide = null;
                pptNS.ShapeRange shapeRange = null;
    
                xlNS.ApplicationClass excelApplication = null;
                xlNS.Workbook excelWorkBook = null;
                xlNS.Worksheet targetSheet = null;
                xlNS.ChartObjects chartObjects = null;
                xlNS.ChartObject existingChartObject = null;
                xlNS.Range destRange = null;
    
                string paramPresentationPath = @"D:\test\Chart Slide.pptx";
                string paramWorkbookPath = @"D:\test\NPS.xlsx";
                object paramMissing = Type.Missing;
    
    
                try
                {
                    // Create an instance of PowerPoint.
                    powerpointApplication = new pptNS.ApplicationClass();
    
                    // Create an instance Excel.          
                    excelApplication = new xlNS.ApplicationClass();
    
                    // Open the Excel workbook containing the worksheet with the chart
                    // data.
                    excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing, paramMissing,
                                    paramMissing, paramMissing);
    
                    // Get the worksheet that contains the chart.
                    targetSheet =
                        (xlNS.Worksheet)(excelWorkBook.Worksheets["Spain"]);
    
                    // Get the ChartObjects collection for the sheet.
                    chartObjects =
                        (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));
    
    
    
                    // Create a PowerPoint presentation.
                    pptPresentation = powerpointApplication.Presentations.Add(
                                        Microsoft.Office.Core.MsoTriState.msoTrue);
    
                    // Add a blank slide to the presentation.
                    pptSlide =
                        pptPresentation.Slides.Add(1, pptNS.PpSlideLayout.ppLayoutBlank);
    
                    // capture range
                    //var writeRange = targetSheet.Range["A1:B15"];
                    destRange = targetSheet.get_Range("A1:B15");
                    pptSlide.Select();
                    pptSlide.Design.Application.ActiveWindow.View.Paste();
    
                    //copy range
                    //destRange.Copy();
    
                    // Paste the chart into the PowerPoint presentation.
                    //shapeRange = pptSlide.Shapes.Paste();
    
                    //var table = pptSlide.Shapes.AddTable();
                    // Position the chart on the slide.
                    //shapeRange.Left = 60;
                    //shapeRange.Top = 100;
    
                    // Get or capture the chart to copy.
                    //existingChartObject = (xlNS.ChartObject)(chartObjects.Item(1));
    
    
                    // Copy the chart from the Excel worksheet to the clipboard.
                    //existingChartObject.Copy();
    
                    // Paste the chart into the PowerPoint presentation.
                    //shapeRange = pptSlide.Shapes.Paste();
                    //Position the chart on the slide.
                    //shapeRange.Left = 90;
                    //shapeRange.Top = 100;
    
                    // Save the presentation.
                    pptPresentation.SaveAs(paramPresentationPath,
                                    pptNS.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
                                    Microsoft.Office.Core.MsoTriState.msoTrue);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    // Release the PowerPoint slide object.
                    shapeRange = null;
                    pptSlide = null;
    
                    // Close and release the Presentation object.
                    if (pptPresentation != null)
                    {
                        pptPresentation.Close();
                        pptPresentation = null;
                    }
    
                    // Quit PowerPoint and release the ApplicationClass object.
                    if (powerpointApplication != null)
                    {
                        powerpointApplication.Quit();
                        powerpointApplication = null;
                    }
    
                    // Release the Excel objects.
                    targetSheet = null;
                    chartObjects = null;
                    existingChartObject = null;
    
                    // Close and release the Excel Workbook object.
                    if (excelWorkBook != null)
                    {
                        excelWorkBook.Close(false, paramMissing, paramMissing);
                        excelWorkBook = null;
                    }
    
                    // Quit Excel and release the ApplicationClass object.
                    if (excelApplication != null)
                    {
                        excelApplication.Quit();
                        excelApplication = null;
                    }
    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
    
                }

    Wednesday, February 1, 2017 10:30 AM
  • Hi Mou_inn,

    which mode is active when you run the code?

    it is possible that file is in presentation mode or in print preview mode or is not visible.

    in that case you can get this error.

    I find some issue with your above code.

    to place the data at particular location, you can set top and left property of shape after pasting the data.

    example:

     private void Form1_Load(object sender, EventArgs e)
            {
                Excel.Application objExcel = new Excel.Application();
                object missing = Type.Missing;
                Excel.Workbook wk = objExcel.Workbooks.Open(@"C:\Users\v-padee\Desktop\chartdata.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                objExcel.Visible = true;
                Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
                Excel.Range range = ws.Application.get_Range("A1:D4", missing);
                range.Copy(missing);
    
                PowerPoint.Application objPower = new PowerPoint.Application();
                objPower.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                PowerPoint.Presentation presentation = objPower.Presentations.Open(@"C:\Users\v-padee\Desktop\Presentation11.pptm", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);
                PowerPoint.Slide slide = presentation.Slides[1];
                slide.Select();
                slide.Design.Application.ActiveWindow.View.Paste();
                slide.Shapes[1].Left=100;
                slide.Shapes[1].Top = 100;
            }

    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.

    • Marked as answer by Sudip_inn Friday, February 3, 2017 1:15 PM
    Thursday, February 2, 2017 8:26 AM
    Moderator
  • HI Team,
    I am trying to copy the powerpoint table and chart from excel by providing the slide number. Only few slides need to copy the tables/charts from excel and then save the entire ppt file.Can i know the options?
    Option 1 : use Excel.Application objExcel = new Excel.Application();?
    Option 2? OpenXML ?
    Do you have any samples? or references?
    Friday, September 6, 2019 5:29 PM
  • this line is not clear.

    PowerPoint.Presentation presentation = objPower.Presentations.Open(@"C:\Users\v-padee\Desktop\Presentation11.pptm", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);

    Saturday, September 7, 2019 7:36 PM