我想從一張工作簿中複製5張。然後將所有5張圖片的內容粘貼到不同的工作簿表單中。VSTO excel粘貼失敗
第一糊劑是成功的,那麼它試圖將其產生錯誤的第二板粘貼:
The information cannot be pasted because the copy area and the paste area are not the same size or shape. Try one of the following:
- Click a single cell, and then paste.
- Select a rectangle that's the same size and shape, and then paste.
以下是我的代碼:
int rowCount = 0;
app = Globals.ThisAddIn.Application;
sourceBook = app.ActiveWorkbook;
targetBook = app.Workbooks.Add(Type.Missing);
targetSheet = targetBook.Worksheets.Add(Type.Missing);
targetSheet.Name = "Merge Result";
foreach (Excel.Worksheet sheet in sourceBook.Worksheets)
{
Excel.Range workSheetRange = sheet.UsedRange;
workSheetRange.Copy(Type.Missing);
Excel.Range pasteStartCell = (Excel.Range)targetSheet.Cells[rowCount + 1, 1];
Excel.Range pasteEndCell = (Excel.Range)targetSheet.Cells[rowCount + workSheetRange.Rows.Count, workSheetRange.Columns.Count];
Excel.Range pasteArea = targetSheet.get_Range(pasteStartCell, pasteEndCell);
pasteArea.Select();
targetSheet.Paste(Type.Missing, Type.Missing);
rowCount = rowCount + workSheetRange.Rows.Count;
}
我也試圖與
Excel.Range pasteStartCell = (Excel.Range)targetSheet.Cells[rowCount + 1, 1];
pasteStartCell.Select()
然後粘貼,相同的錯誤消息。
有誰知道我錯了哪一部分?
嗨jbarker2160:謝謝你對我用Range.PasteSpecial嘗試過的建議,但仍然得到同樣的錯誤。我需要在第二個副本之前清除剪貼板嗎? – 2014-10-10 02:08:12
不要發佈不作爲答案工作的更改 - 使用更新編輯您的問題是正確的過程。 – 2014-10-10 02:45:18