I'm working on a C# Application thats interacts with an Excel instance using excel interop.dll v11.0. I'm using the following code to copy a chart from the excel worksheet to the clipboard:
public Image ReadChart(Chart chartAccess) {
try {
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Sheets[chartAccess.Sheet.Name];
Microsoft.Office.Interop.Excel.ChartObject chart = sheet.ChartObjects(chartAccess.Name);
chart.Chart.ChartArea.Copy(); // exception gets thrown here
return System.Windows.Forms.Clipboard.GetImage();
} catch (COMException) {
// display error dialog etc...
}
This worked fine with Excel 2007. However since switching to Excel 2013 the function ChartArea.Copy() results in the following COMExceptions being thrown:
Message: "Dimension not valid for chart type"
Stack Trace: System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
Microsoft.Office.Interop.Excel.ChartArea.Copy()
Message: "HRESULT: 0x800A03EC"
Stack Trace: System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
Microsoft.Office.Interop.Excel.ChartArea.Copy()
This only happens with excel sheets of the old .xls format, xlsm / xlsx files work fine. Using newer versions of interop.dll (v14.0 and v15.0) didn't help.
Any help is appreciated!
EDIT:
I resolved the Problem using the following workaround:
// ...
chart.Chart.activate();
chart.Chart.Export(filename, "PNG", false);
using (Stream reader = File.OpenRead(filename)) {
image = Image.fromStream(stream);
}
return image;
chart.Chart.ChartArea.Cut(); instead of copy you can use cut it wont give any exception
The documentation for Copy shows that there is a difference between versions 2003 and 2010.
2003:
2010:
As you can see, the arguments were optional in 2003, and mandatory in later version.