Reputation: 667
Below Is Created Module1.bas and Storing the code on it. i would like to add the below code to trigger the macro From "Thisworkbook".
How can i add Workbook_Open() in Thisworkbook ?
sCode ="Private Sub Workbook_Open()\r\n" + "UserForm1.Show\r\n" + "end sub"
using Excel = Microsoft.Office.Interop.Excel;
using VBIDE = Microsoft.Vbe.Interop;
VBIDE.VBComponent oModule;
String sCode;
oModule = wb.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
sCode ="Private Sub Workbook_Open()\r\n" +
"UserForm1.Show\r\n" +
"end sub"
oModule.CodeModule.AddFromString(sCode);
Upvotes: 1
Views: 832
Reputation: 667
var workbookMainModule = wkBk.VBProject.VBComponents.Item("ThisWorkbook");
workbookMainModule.CodeModule.AddFromString(sCode);
Upvotes: 0