dimanche 19 avril 2015

Worksheets.copy(before:,After:) error c#

Hello everyone and I'm working on a project that is giving me some trouble. I will show you the code first then explain.



xl.Application xlApp = (xl.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

xl.Worksheet ws = Globals.ThisWorkbook.Sheets["Sheet2"];
xl.Workbook newWorkBook = null;
newWorkBook = xlApp.Workbooks.Add();
ws.Copy(Before: newWorkBook.Sheets["Sheet1"],After:Type.Missing);


The error that I receive is as follows:



An exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll but was not handled in user code


Additional Information: Unable to get the copy property of the worksheet class



But when I just run code like



ws.copy();


It runs just fine, switching to a new workbook. I would use this as that is what I'm wanting to do, I just would like to be able to copy 10 worksheets to a new workbook that is the reason that I'm creating a new workbook. Any Ideas?


Aucun commentaire:

Enregistrer un commentaire