none
Programatically Insert Excel Worksheet into PowerPoint C# RRS feed

  • Question

  • I am programmatically creating an Excel workbook in a Windows forms application (in C#), that contains a table of data and a chart which represents the data (in a single worksheet). I am currently using Office 2003.

    My customer would like me to provide this same view of data in PowerPoint. In PowerPoint, I can manually insert an Excel workbook by choosing from the menu bar, Insert/Object, then "Create from file", then select "Microsoft Excel Worksheet". I select the Excel workbook that was previously created and the worksheet is displayed on the PowerPoint slide. This will satisfy my customer's requirement, but I need to perform this task programmatically.

    Would somebody be able to help me understand how to insert an Excel workbook into PowerPoint (Office 2003) programmatically?

    Thanks,

    Kevin

    Friday, June 22, 2012 8:10 PM

Answers

  • Hi Kevin,

    Thanks for posting in the MSDN Forum.

    This is a sample, it works fine on my side. I hope it can help you.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    namespace PPTEmbedWorkSheet
    {
        public partial class Form1 : Form
        {
            private OpenFileDialog OFD = new OpenFileDialog();
            private string _PPTPath;
            public Form1()
            {
                InitializeComponent();
                OFD.Multiselect = false;
                button2.Enabled = false;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                OFD.Filter = "PowerPoint Presentation (2003)|*.ppt";
                OFD.ShowDialog();
                _PPTPath = OFD.FileName;
                button2.Enabled = true;
            }
            private void button2_Click(object sender, EventArgs e)
            {
                OFD.Filter = "Excel Workbook (2003)|*.xls";
                OFD.ShowDialog();
                string Path = OFD.FileName;
                string temp = AppDomain.CurrentDomain.BaseDirectory + "temp.xls";
                File.Copy(Path, temp, true);
                Excel.Application xlApp = new Excel.Application();
                var missing = Type.Missing;
                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(temp, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets[2];
                xlApp.Visible = true;
                
                foreach (var obj in xlWorkbook.Sheets)
                {
                    Excel.Worksheet ws = (Excel.Worksheet)obj;
                    if (!ws.Name.Equals(xlWorksheet.Name))
                    {
                        ws.Delete();
                    }
                }
                xlWorkbook.Save();
                xlWorkbook.Close(missing, missing, missing);
                xlApp.Quit();
                PowerPoint.Application ppApp = new PowerPoint.Application();
                PowerPoint.Presentation ppP = ppApp.Presentations.Open(_PPTPath, Office.MsoTriState.msoFalse,
                    Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse);
                ppApp.Visible = Office.MsoTriState.msoTrue;
                ppP.NewWindow();
                PowerPoint.Slide ppS = ppP.Slides[2];
                try
                {
                    PowerPoint.Shape shap = ppS.Shapes.AddOLEObject(0, 0, 200, 100,
                        "", temp, Office.MsoTriState.msoFalse, temp, 1,
                        "test", Office.MsoTriState.msoFalse);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    File.Delete(temp);
                    Close();
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 5:51 AM
    Moderator

All replies

  • Hi Kevin,

    Thanks for posting in the MSDN Forum.

    This is a sample, it works fine on my side. I hope it can help you.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    namespace PPTEmbedWorkSheet
    {
        public partial class Form1 : Form
        {
            private OpenFileDialog OFD = new OpenFileDialog();
            private string _PPTPath;
            public Form1()
            {
                InitializeComponent();
                OFD.Multiselect = false;
                button2.Enabled = false;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                OFD.Filter = "PowerPoint Presentation (2003)|*.ppt";
                OFD.ShowDialog();
                _PPTPath = OFD.FileName;
                button2.Enabled = true;
            }
            private void button2_Click(object sender, EventArgs e)
            {
                OFD.Filter = "Excel Workbook (2003)|*.xls";
                OFD.ShowDialog();
                string Path = OFD.FileName;
                string temp = AppDomain.CurrentDomain.BaseDirectory + "temp.xls";
                File.Copy(Path, temp, true);
                Excel.Application xlApp = new Excel.Application();
                var missing = Type.Missing;
                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(temp, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets[2];
                xlApp.Visible = true;
                
                foreach (var obj in xlWorkbook.Sheets)
                {
                    Excel.Worksheet ws = (Excel.Worksheet)obj;
                    if (!ws.Name.Equals(xlWorksheet.Name))
                    {
                        ws.Delete();
                    }
                }
                xlWorkbook.Save();
                xlWorkbook.Close(missing, missing, missing);
                xlApp.Quit();
                PowerPoint.Application ppApp = new PowerPoint.Application();
                PowerPoint.Presentation ppP = ppApp.Presentations.Open(_PPTPath, Office.MsoTriState.msoFalse,
                    Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse);
                ppApp.Visible = Office.MsoTriState.msoTrue;
                ppP.NewWindow();
                PowerPoint.Slide ppS = ppP.Slides[2];
                try
                {
                    PowerPoint.Shape shap = ppS.Shapes.AddOLEObject(0, 0, 200, 100,
                        "", temp, Office.MsoTriState.msoFalse, temp, 1,
                        "test", Office.MsoTriState.msoFalse);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    File.Delete(temp);
                    Close();
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 5:51 AM
    Moderator
  • Tom:

    Thanks for the information! It helped me solve my requirement.

    Kevin


    Kevin Lowery

    Thursday, June 28, 2012 8:20 PM