Excel paste error.

D

Damo

I'm using C# to copy information into worksheets in an existing workbook
(using Excel 2007 interop). I have a problem when I try and paste a range of
data after clearing a worksheet. The error message reads HRESULT: 0x800A03EC

I've tried using ClearContents and PasteSpecial with the same result. If I
omit the call to Clear there is no exception thrown. Maybe I should
delete/re-insert the worksheet?Any suggestions as to why this problem occurs
will be greatly appreciated.

The code snippet:

Microsoft.Office.Interop.Excel.Application excel = new ApplicationClass();

Workbook targetWorkbook = excel.Workbooks.Open("c:\\result.xls", 0, false,
5, "", "", true, XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
Sheets targetSheets = targetWorkbook.Sheets;

Workbook inputWorkbook = excel.Workbooks.Open("c:\\input.xls", 0, false, 5,
"", "", true, XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
Sheets inputSheets = inputWorkbook.Sheets;

Worksheet inputWorksheet = (Worksheet) inputSheets.get_Item(0);
Range inputRange = inputWorksheet.get_Range("A1", "D12");
inputRange.Copy(Type.Missing);

Worksheet targetWorksheet = (Worksheet) targetSheets.get_Item(0);
targetWorksheet.Cells.Clear();

Range targetRange = targetWorksheet.get_Range("A1", "D12");
targetWorksheet.Paste(targetRange, false);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top