Hi I’m currently trying to copy a macro from one workbook to another workbook.
For example lets say I have…
- Workbook A, with Worksheets named “Worksheet1” and “Worksheet2” each having VBA code (Macros)
and I have
- Workbook B, with Worksheets named “Worksheet3” , “Worksheet1”, and “Worksheet2” with no VBA code (Macros).
I want to be able to copy the Macros from “Worksheet1” and “Worksheet2” from Workbook A and copy them to corresponding “Worksheet1” and “Worksheet2” in Workbook B.
Currently when I try to grab the name of the worksheet that the VBAModule is associated with using …
var outputPath = @"C:\Users\AC\Desktop\xlsm\test.xlsm";
FileStream newFstream = new FileStream(newPath, FileMode.Open);
var workbook = new Workbook(fstream);
VbaModuleCollection modules = workbook.VbaProject.Modules;
for (int i = 0; i < modules.Count; i++)
{ Console.WriteLine(modules[i].**strong text**Name);
}
Instead of the names “Worksheet1” and “Worksheet2”, I get Sheet1 and Sheet2 which seem to be based on when the worksheets were added to the workbook.
Is there a better way to identify which VBAModules are associated with which Worksheets rather then arbitrary names such as Sheet1, Sheet2, … Sheet10.