0
votes

I need to merge the worksheets of some workbooks into one new workbook. What I tried is this, but I am getting "Unable to get the Copy property of the Worksheet class". What am I doing wrong? Thank you!

foreach (var sourceFileName in sourceFileNames)
{
    var sourceWorkbook = GetWorkbook(sourceFileName);

    var sourceSheet = (Worksheet)sourceWorkbook.Worksheets[1];
    var lastSheetInDestinationWorkbook = (Worksheet)_destinationWorkbook.Worksheets[_destinationWorkbook.Worksheets.Count];
    sourceSheet.Copy(After: lastSheetInDestinationWorkbook);

    sourceWorkbook.Close();
}
((Worksheet)_workbook.Sheets[1]).Delete();
_workbook.Save();
1
I've seen that, thanks, but no.IngoB
My crystal ball says that GetWorkbook() creates another Application object.Hans Passant
I'm not a big fan of psychic debugging solutions. You've got some editing to do to make the mishap obvious, post the solution as well and mark it as the answer.Hans Passant

1 Answers

2
votes

My fault was to load the workbooks in different Application objects. Using the same Application object solved the problem.

Thanks @HansPassant to point me there.