Answered by:
AddChart gives COM error

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(); } } } }
- Moved by Edward8520Microsoft contingent staff Tuesday, May 24, 2016 3:15 AM
Monday, May 23, 2016 2:30 PM
Answers
-
Hi,
Check this article and try the mentioned methods to solve your problem:
As Edward suggested, you could also use Open XML SDK to achieve this function. Apart from this, there are also other alternative ways like Insert Chart to PowerPoint documents in C#
Hope my answer could help you.
- Proposed as answer by Edward8520Microsoft contingent staff Thursday, May 26, 2016 6:00 AM
- Marked as answer by Edward8520Microsoft contingent staff Thursday, June 2, 2016 5:44 AM
Wednesday, May 25, 2016 2:54 AM -
Hi Kevin,
Could you share us why you want to add a chart when PowerPoint is hide? I made a test with your code, and I could reproduce your issue.
As my understanding, Office automation is used to achieve what we could achieve manually and help us to use PowerPoint more convenient. You could assume that if you hide PowerPoint application windows, how you add the Cart.
If you want to add a chart without powerpoint open, I would suggest you use Open XML SDK to achieve your requirement.
Best Regards,
EdwardWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Edward8520Microsoft contingent staff Thursday, May 26, 2016 6:00 AM
- Marked as answer by Edward8520Microsoft contingent staff Thursday, June 2, 2016 5:44 AM
Tuesday, May 24, 2016 3:15 AM
All replies
-
Hi Kevin,
Could you share us why you want to add a chart when PowerPoint is hide? I made a test with your code, and I could reproduce your issue.
As my understanding, Office automation is used to achieve what we could achieve manually and help us to use PowerPoint more convenient. You could assume that if you hide PowerPoint application windows, how you add the Cart.
If you want to add a chart without powerpoint open, I would suggest you use Open XML SDK to achieve your requirement.
Best Regards,
EdwardWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Edward8520Microsoft contingent staff Thursday, May 26, 2016 6:00 AM
- Marked as answer by Edward8520Microsoft contingent staff Thursday, June 2, 2016 5:44 AM
Tuesday, May 24, 2016 3:15 AM -
Hi,
Check this article and try the mentioned methods to solve your problem:
As Edward suggested, you could also use Open XML SDK to achieve this function. Apart from this, there are also other alternative ways like Insert Chart to PowerPoint documents in C#
Hope my answer could help you.
- Proposed as answer by Edward8520Microsoft contingent staff Thursday, May 26, 2016 6:00 AM
- Marked as answer by Edward8520Microsoft contingent staff Thursday, June 2, 2016 5:44 AM
Wednesday, May 25, 2016 2:54 AM