Preserve VBA on Copy

I have a couple buttons attached to some VBA code on a template workbook and worksheet. I’m taking the worksheet with the buttons and copying it several times. The button is on all copied sheets, but the VBA code for it is not. Is there a way to do this?

This is how I’m copying:

Worksheet sheet = workbook.Worksheets[workbook.Worksheets.Add()];
sheet.Copy( templateSheet );

Thanks.

Hi,

Thanks for considering Aspose.

Could you post your template file with VBA code, so that we may check and figure out the issue soon.

Thank you.

Please use Workbook.Copy method. Worksheet.Copy method doesn’t preserve VBA for we cannot seperate VBA code. We only support to copy them as a whole.

Here is the file. The sheet named “Template” is the one that gets copied several times. I need the VBA code for the button clicks to be copied also.

I only want one sheet to be copied. Can Workbook.Copy do a single sheet?

Hi,

As Laurence has told you that you should use Workbook.Copy method as Worksheet.Copy method doesn't preserve VBA for to seperate VBA code. I think you can use Workbook.Copy() method and then remove the undesired worksheets from the workbook.

E.g.,

Workbook workbook = new Workbook();

workbook.Open("d:\\test\\QuikTemplate.xls");

Workbook wkb = new Workbook();

wkb.Copy(workbook);

wkb.Worksheets.RemoveAt(0);

wkb.Save("d:\\test\\outBook.xls");

Thank you.