Free Support Forum - aspose.com

VBA Modules

Is there any way to combine two workbooks together that contain VBA modules and classes? If the same class or module exists in both workbooks we would only want one.

For example:
Workbook 1 contains
S1,
M1,
M2,
C1

Workbook 2 contains
S1,
M1,
C2

The resulting workbook would contain
S1,
M1,
M2,
C1,
C2


This message was posted using Page2Forum from Workbook.Combine Method - Aspose.Cells for .NET

Hi,

Thanks for your interest in Aspose.Cells.

Yes, it is possible. You can join the two workbooks. Please see the code below, it basically copies all the worksheets from source workbook to destination workbook. You can modify it a little and copy all the source worksheets without deleting the destination workbook sheets.

Let me know if you encounter any problem.

C#


string dirPath = @“F:\Downloads”;


Workbook sourceWorkbook = new Workbook(dirPath + “source.xlsx”);

Workbook destWorkbook = new Workbook(dirPath + “destination.xlsx”);


//You should not delete the existing worksheets though
destWorkbook.Worksheets.Clear();


foreach (Worksheet sourceSheet in sourceWorkbook.Worksheets)
{
destWorkbook.Worksheets.Add();
destWorkbook.Worksheets[destWorkbook.Worksheets.Count - 1].Copy(sourceSheet);
}

destWorkbook.Save(dirPath + @“destOutput.xlsx”, SaveFormat.Xlsx);

Hi,

I think you may also use Workbook.Combine() method to combine/merge the two Excel files/workbooks.

Workbook MasterTemplate = new Workbook(“e:\test\Main.xls”);
Workbook SkeletonTemplate = new Workbook(“e:\test\Second.xls”);
MasterTemplate.Combine(SkeletonTemplate);
MasterTemplate.Save(“e:\test\Output1.xls”);

If you find the issue, kindly do give us your template files and output file to show the issue. Also you may point out the issue regarding VBA in the output file. Moreover, you may provide your desired file having your desired VBAs. We will check it soon.

Thank you.

Thank-you for your quick reply. Unfortunately, I am looking at combining the VBA modules and classes.



If I use the method you describe I only get the modules from the destWorkbook.

If I use this method, I only get the modules from workbook2



Workbook workbook1 = new Workbook(“C:\tempCJLRD\A.xls”);

Workbook workbook2 = new Workbook(“C:\tempCJLRD\B.xls”);



workbook2.Combine(workbook1);



workbook2.Save(“C:\tempCJLRD\Result.xls”);

With the same code but using a “Copy” method I only get the modules from workbook1.

Hi,


I am afraid, we currently do not support this feature [Combining of VBA modules]. With Aspose.Cells you can only preserve or remove the VBA code from workbook.
Thank you for understanding.