Search code examples
c#chartspowerpointoffice-interop

Problem with generated PowerPoint Presentation Charts in C# with office.interop


I am working on a small C# project, that creates a PowerPoint presentation from Jira data. The program worked fine, until like 2 weeks ago a new error occurred (no changes to coding where made in this time).

What I am doing exactly is: I have a PowerPoint Template with a pie chart. I want to edit the Data of the pie chart in my program and at the end I save the presentation as new presentation in a folder the user specified.

The bug that occurs is the following: The data shows correctly in the PPT presentation and die pie chart also works. However, if you right klick the pie chart and select "edit data", die chart resets to the values specified in the template file (names are reset and values also change to "1", like it is in my template). This is a clearly new behavior (it never happened since 2 weeks ago).

The code I use to edit the data behind the pie chart looks like this:

            // using this libraries to acces powerpoint / excel:
            using PowerPoint = Microsoft.Office.Interop.PowerPoint;
            using Excel = Microsoft.Office.Interop.Excel;

            PPTPres.Slides[DiagrammPos[0]].Shapes[DiagrammPos[1]].Chart.ChartData.Activate();
            Excel.Workbook ExcelWB = (Excel.Workbook)PPTPres.Slides[DiagrammPos[0]].Shapes[DiagrammPos[1]].Chart.ChartData.Workbook;
            Excel.Application ExcelApp = ExcelWB.Application;
            ExcelApp.Visible = false;
            Excel.Worksheet DiagData = (Excel.Worksheet)ExcelWB.Worksheets[1];

            // example how DiagData is used in the programm:
            DiagData.Cells[Count, 1].Value = FileSet.GetName();
            DiagData.Cells[Count, 2].Value = TimeTotal;

I tried to manually save the excel workbook - object like this but I get an exception if I try to do this:

        try
        {
            ExcelWB.Save();
        } catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

The Exception is a HRESULT:0x800A03EC - Exception. It looks like this:

Screenshot of the Exception

For reference, here are some screenshots of the behavior in the PowerPoint presentation:

Diagram after my program ran (everything looks good):

Diagram after my program ran (everything looks good)

Screenshot of the option, that destroys everything:

Screenshot of the option, that destroys everything

Screenshot of the reset chart:

Screenshot of the reset chart

Is there a way to prevent the pie chart from being reset?

EDIT: I tried multiple things, but nothing worked out:

  • Refreshing the Diagramm after all changes are done.
  • Closing the ExcelWB - Object with the .close() method
  • Changing the Template (it resets to the changed template if i do this)
  • Saving the changed Template before saving the PPT as new Presentation (still resets everything)

I am realy stuck with this issue.


Solution

  • I found out how to fix this on my own:

    My first idea (saving the Excel file) seems to be the right guess. However, the .save() method does not work (throws exception).

    The trick is: you can save with another method that is working fine, the method to close the excel file has a boolean, that if set to true, saves all changes. Everything i needed to do is adding the following in my file:

    ExcelWB.Close(true);