vsto - How to insert existing worksheet in Excel using C#? -
i trying insert existing sheet excel current worksheet. but, instead inserting current worksheet, creates new worksheet. missing in code?
i using this:
workbook wkactive = globals.thisaddin.application.activeworkbook; objbook = globals.thisaddin.application.workbooks.open(idstemplatepath, 0, true, 5, "", "", true, xlplatform.xlwindows, "\t", false, false, 0, true, false, false); wkactive.sheets.add(objbook.sheets[1], type.missing, type.missing, type.missing); wkactive.save(); wkactive.close();
from mvp:
i believe worksheet.copy method want. assuming using vsto, following code demonstrates 1 way copy worksheet non-vsto workbook workbook in vsto customization. particular example copies first worksheet in non-vsto workbook named "myworkbook.xls" vsto workbook, after "sheet3".
excel.workbook workbook = globals.thisworkbook.application.workbooks.open(@"c:\myworkbook.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); excel.worksheet worksheet = workbook.sheets[1] excel.worksheet; worksheet.copy(missing, globals.sheet3.innerobject);
the code first opens non-vsto workbook named "myworkbook.xls". code copies first worksheet of workbook vsto workbook calling copy method of non-vsto worksheet, , passing in vsto worksheet want copy non-vsto worksheet after.
the tricky vsto-specific part of code fact passes in innerobject property instead of globals.sheet3 copy method. because copy expects microsoft.office.interop.excel.worksheet, , vsto worksheet (that is, globals.sheet3) microsoft.office.tools.excel.worksheet. innerobject property exposes microsoft.office.interop.excel.worksheet underlies microsoft.office.tools.excel.worksheet.
Comments
Post a Comment