المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : exception trying to insert a range to an Excelsheet



C# Programming
07-02-2009, 03:30 PM
Hello there,

I am programming C# using MS Visual Studio Professional Edition 2008 and MS Office Excel 2007.
My Problem is that I want to mark a range, copy and insert it at the same position shifting existing cells down. I can do this with the Excel GUI - no problem. But programmatically I get a System.Runtime.InteropServices.COMException, telling me that the Insert-method of the Range-object could not be executed (German text:"Die Insert Methode des Range-Objektes konnte nicht ausgeführt werden.").

The range I want to copy has got some special formats (font, color, some merged cells), and I need these for each entry I have to insert.

I am trying this code

Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets["mySheet"]; // wb is the actual workbook
Excel.Range range = sheet.get_Range(sheet.Cells[16, 1], sheet.Cells[18, 37]);// this is "A16:AK18"
range.Insert(Excel.XlInsertShiftDirection.xlShiftDown, range);


Here come the questions:
Why can I not insert the range? What are possible reasons?
How can I insert the desired range?

Thanks in anticipation!
Harry

no plan,
no signature