I had a problem with copying ExcelWorkSheetand the corresponding CodeModulecopied sheet. Let me explain:
Scenario:
I have an excel file with a macro (.xlsm) and I use this excel file as a template to create another excel file (the data will be copied to a new file).
So, first I take some data from the database and open the Excel ( .xlsm) file .
At some point, some entries should be in different sheets, this is where "WorkSheets.Add ()" is included.
var newSheet = workbook.Worksheets.Add("someName", templateSheet);
After copying the template sheet (which contains the VBA code, which I also want to duplicate), I am having problems.
It seems that the "CodeModule" of the sheet just created is equal to one of the template by name AND by reference.
I.e; all property values are the same and: workbook.VbaProject.Modulesonly contains the source modules of the template file code, and not new for the new one newSheet.
Even worse, if I want to link the new one CodeModuleas follows:
workbook.VbaProject.Modules.AddModule("test");
newSheet.CodeModule.Name = "test";
both options newSheet.CodeModuleand templateSheet.CodeModulemounted on null(well, Nothingactually, because I'm using VB.Net).
So the question is: is this a mistake, or am I doing something wrong here? And even better: can you steer a path to execute this scenario?