This project has moved. For the latest updates, please go here.

Sheet.Copy

Oct 26, 2012 at 8:21 PM

Hi,

is it possible to copy a Sheet from a Workbook to another?

I used the microsoft.office.interop before and there i could use the command Workbook1.Sheets[integer].Copy(After: Workbook2.Sheets[integer])

I have to programm it for all versions of Excel. So i have found the netoffice-API.

Maybe one of you could help me.

Coordinator
Oct 27, 2012 at 2:09 PM

hello!

yes, of course. its basicly the same in NetOffice.

Visual Basic Example:

workBook.Worksheets(1).Copy(Nothing, workBook.Worksheets(2))


Sebastian

Oct 28, 2012 at 2:21 PM

Ahh okay, thanks.

My mistake: In microsoft.office i have to use square bracket to choose the index of sheet. Now it's round bracket.

Thank you Sebastian.

 

Psycho_D

Oct 28, 2012 at 4:23 PM

Hmm, i try to use the code above (Workbook.Worksheets(1).Copy(...)). But visual studio says i can not use Workbook.Worksheets like a method. When i write the command with square bracket (Workbook.Worksheets[1].Copy(...)) it says Copy is not a method from Worksheets.

Maybe i make any mistake? 

And my programming language is c#.

If it is helpfull to post my code, i will do it.

Coordinator
Oct 28, 2012 at 4:30 PM

yes, your first post looks like vb. thats the reason for the vb example.
the problem is: Workbook.Worksheets[1]
the definition in the COM Type Library for the return value is Excel.Sheet and you have to convert the type at runtime to Excel.Worksheet.

Excel.Worksheet sheet1 = Workbook.Worksheets[1] as Excel.Worksheet;
Excel.Worksheet sheet2 = Workbook.Worksheets[2] as Excel.Worksheet;

this is not necessary in visual basic because vb is not strong type safe. you can use any type similar to the dynamic type in C#

Sebastian