I use C # Excel interop and I want to create a copy of the chart from one sheet, but I want this copy on another sheet. I tried the following:
Excel.ChartObject chartTemplate = (Excel.ChartObject)sheetSource.ChartObjects("chart 1");
object o = chartTemplate.Duplicate();
Excel.ChartObject chart = (Excel.ChartObject)sheetSource.ChartObjects("chart 2");
chart.Name = "Skew" + expiry.ToString("MMMyy");
range = sheetDestination.Range["T" + chartRowCoutner.ToString()];
chart.Chart.Location(Excel.XlChartLocation.xlLocationAsObject, range);
But when I try to do this, the last line throws an error:
An unhandled exception of type "System.Exception" occurred in the projectname.exe file
Additional information: Error reading Excel file C: \ ... path to file ... \ template.xlsx: value does not fall into the expected range.
I also tried passing a sheet instead of a range:
chart.Chart.Location(Excel.XlChartLocation.xlLocationAsObject, sheetDestination);
but it gives the same error. I can not understand the cause of the error or how to fix / work around it.
I try to avoid transferring the clipboard to this, but even if I try to copy and paste, I can still paste it only as an image, which is really not perfect:
Excel.ChartArea chartArea = chart.ChartArea;
chartArea.Copy();
range = sheetDestination.Range["T" + chartRowCoutner.ToString()]; // Note that chart is not on the sheet sheetDestination
range.PasteSpecial(Excel.XlPasteType.xlPasteAll);
, , - VBA, interop. , , , interop .