locked
AddChart gives COM error RRS feed

  • Question

  • Hi,

        I'm using Windows 7, Office 2013, Visual Studio 2012.

        I'm trying to add a chart to a Powerpoint presentation but getting a COM error, using a C# program. It works fine if I open the presentation with the WithWindow parameter set to MsoTriState.msoTrue but fails with a COM error if the parameter is set to MsoTriState.msoFalse. 

        The COM error I get is 'Error HRESULT E_FAIL has been returned from a call to a COM component.' with an HRESULT of -2147467259, with no other information. Below is some code to reproduce the problem.

        Has anyone else seen this before or can anyone suggest a way to proceed please?

        Thanks,

        Kevin

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using IOPP = Microsoft.Office.Interop.PowerPoint;
    using gr = Microsoft.Office.Interop.Graph;
    using xl = Microsoft.Office.Interop.Excel;
    using Core = Microsoft.Office.Core;
    
    namespace Powerpointchart
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                IOPP.Application powerpoint = new IOPP.Application();
                var presentations = powerpoint.Presentations;
                IOPP.Presentation pres = presentations.Open("C:\\templates\\PPTChart.pptx", 
                                                            Microsoft.Office.Core.MsoTriState.msoTrue, 
                                                            Microsoft.Office.Core.MsoTriState.msoTrue, 
                                                            Microsoft.Office.Core.MsoTriState.msoFalse);
    
                try
                {
                    //Instantiate slide object
                    Microsoft.Office.Interop.PowerPoint.Slide objSlide = null;
    
                    //Access the first slide of presentation
                    objSlide = pres.Slides[1];
    
                    IOPP.Chart ppChart = objSlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xl3DColumn, 20F, 30F, 400F, 300F).Chart;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
                finally
                {
                    //Close Workbook and presentation
                    pres.Application.Quit();
                }
            }        
        }
    }

    Monday, May 23, 2016 2:30 PM

Answers

All replies