From my experience the excel file only get closed when you save/close de presentation. After activate the chartdata I make the excel application invisible.
I have more developments in my question, I'm going to make sure that all windows updates are installed (because I run my sample in a different PC and all went well) and after that, I post here my sample.
Powerpoint chart data not saved
Hello,
I've some office automation in c# to change slides in powerpoint.
Since some windows updates (didn't find out witch one yet), the changed chart data in the included excel, won't be saved.
It seems fine the saved powerpoint file, but, when tried to edit chart data, changes to the old values.
Does anyone have the same behavior?
4 answers
Sort by: Most helpful
-
Ricardo de Sousa 1 Reputation point
2021-05-24T07:25:44.213+00:00 -
Ricardo de Sousa 1 Reputation point
2021-05-24T09:24:36.777+00:00 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 Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;namespace PowerpointAutomation
{
public partial class Form1 : Form
{public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "pptx|*.pptx"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { MessageBox.Show("changing chart data"); string m_filename = openFileDialog1.FileName; string fullPath = System.IO.Path.GetFullPath(m_filename); string fileName = System.IO.Path.GetFileNameWithoutExtension(m_filename); PowerPoint.Application objApp = new PowerPoint.Application(); PowerPoint.Presentations objPresSet = objApp.Presentations; PowerPoint._Presentation objPres = objPresSet.Open(m_filename, MsoTriState./*msoTrue*/msoFalse, MsoTriState./*msoTrue*/msoFalse, MsoTriState.msoFalse); PowerPoint.Slides objSlides = objPres.Slides; foreach (PowerPoint._Slide objSlide in objSlides) { PowerPoint.Shapes objShapes = objSlide.Shapes; foreach (PowerPoint.Shape objShape in objShapes) { switch (objShape.Type) { case MsoShapeType.msoChart: if (objShape.HasChart == MsoTriState.msoTrue) { PowerPoint.Chart mychart = objShape.Chart; PowerPoint.ChartData mydata = mychart.ChartData; mydata.Activate(); ((Excel.Workbook)mydata.Workbook).Application.Visible = false; Excel.Workbook mywkb = mydata.Workbook as Excel.Workbook; Excel._Worksheet mysheet = (Excel._Worksheet)mywkb.Worksheets[1]; Excel.Range usedRange = mysheet.UsedRange; string address = usedRange.Address; usedRange.Clear(); // Excel::Range newCellsRange = mysheet.Cells; int axisCategories = 10; int serieCount = 1; int i = 0; for (i = 1; i <= axisCategories; i++) { newCellsRange.set_Item(1, i + 1, string.Format("cat{0}", i - 1)); } for (i = 1; i <= serieCount; i++) { newCellsRange.set_Item(i + 1, 1, string.Format("serie{0}", i)); } Random random = new Random(15); for (int r = 1; r <= serieCount; r++) { for (int c = 1; c <= axisCategories; c++) { newCellsRange.set_Item(r + 1, c + 1, random.Next(10, 1000)); } } usedRange = mysheet.UsedRange; string sAddress = "=Sheet1!"; sAddress += usedRange.Address; mychart.SetSourceData(sAddress); //just to make sure that is changed string excelFileSaved = fullPath.Replace(fileName+".pptx", "changedexcel.xlsx"); mywkb.SaveCopyAs(@excelFileSaved); } break; default: break; } } } string newFileName = fileName + "_NEW"; string newFullPath = fullPath.Replace(fileName, newFileName); objPres.SaveAs(@newFullPath, PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue); MessageBox.Show("saved"); objPres.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(objPres); objPres = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp); objApp = null; } } }
}
-
Ricardo de Sousa 1 Reputation point
2021-05-25T14:55:32.69+00:00 I don't see nothing wrong with this code, and if you run it all seems ok
but if you try to edit the chart the chart data of the saved slide you'll notice that the data is wrong -
Ricardo de Sousa 1 Reputation point
2021-06-23T14:46:05.02+00:00