Copy vba code to another xlsm file using Open XML from a template xlsm file

nguyenmallard

New Member
I use this code to add VbaPart of the template xlsm file into another .When i open the vb section duplicate entries get added for each sheet as well as the "ThisWorkBook".Below is the screen shot of how it looks in the developer tab
03ocR.png
And below is the code that i use :\[quote\] using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(convertDocumentFile, true)) { VbaProjectPart extendedPart = FindPart(myDoc);\[code\] if (extendedPart != null) myDoc.DeletePart(extendedPart); if (vbaPart != null) myDoc.WorkbookPart.AddPart<VbaProjectPart>(vbaPart); }\[/code\]\[/quote\]//--------------------------------------------------------------------------\[code\] private static VbaProjectPart FindPart(SpreadsheetDocument mainPart) { if (mainPart != null) { foreach (IdPartPair partPair in mainPart.WorkbookPart.Parts) { if (partPair.OpenXmlPart.RelationshipType == _wnsRelationShip.NamespaceName) { return partPair.OpenXmlPart as VbaProjectPart; } } } return null; }\[/code\]Where the variable "convertDocumentFile" is the file where the vbaProject part needs to be added and the "vbaPart" is the vbaProjectPart from the template .Regards,Francis P.
 
Back
Top