I have an Excel workbook with multiple charts inside. I have created a for loop that gets the charts in each sheet. This works without error.
The issues I am having are I cannot import multiple charts, only one and it imports as a picture not a chart.
When I try to import multiple charts, I get a COMException
on this line:
objChart.Copy();
Full error:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code
Additional information: Exception from HRESULT: 0x800A03EC
Here Is my full method:
public int ImportExcelChartsFromWorkbookToSlides(int startingSlideIndex, string workbookPath, string[] slideTitles)
{
int slideIndex = startingSlideIndex;
int titleIndex = 0;
EXCL.Application objExclApp = new EXCL.Application();
EXCL.Workbook objWorkbook = objExclApp.Workbooks.Open(workbookPath, Editable: false);
foreach (EXCL.Worksheet objSheet in objWorkbook.Worksheets)
{
foreach (EXCL.ChartObject objChart in objSheet.ChartObjects())
{
AddTitleOnlySlide(slideIndex);
SetTitleOnlySlideTitle(slideTitles[titleIndex]);
// Copy Chart from Sheet to Slide
objChart.Copy();
PPT.ShapeRange objShapeRange = objSlide.Shapes.Paste();
// TODO PARAMETER
objShapeRange.Left = 10;
objShapeRange.Top = 100;
slideIndex++;
titleIndex++;
}
}
return slideIndex;
}
Does anyone see any issues with this code?
UPDATE
If I change this line:
objChart.Copy();
to this:
objChart.CopyPicture();
I have no issues.. What could be causing this?
I managed to fix this issue by using objChart.CopyPicture();
but this was causing crashes for some users so I ended up doing this:
string chartPath = $@"{workbookPath.Substring(0, workbookPath.LastIndexOf("\\") + 1)}{DateTime.Now.Ticks}.png";
objChart.Chart.Export(chartPath, "PNG", false);
objSlide.Shapes.AddPicture2(chartPath, MsoTriState.msoFalse, MsoTriState.msoTrue, chartPosLeft, chartPosTop, compress: MsoPictureCompress.msoPictureCompressFalse);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.