创建Module1.bas以下并在其上存储代码。我想将以下代码添加到 从“Thisworkbook”中触发宏。
如何在Thisworkbook中添加Workbook_Open()?
sCode =“Private Sub Workbook_Open()\ r \ n”+ “UserForm1.Show \ r \ n”+ “结束子”
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);
答案 0 :(得分:0)
var workbookMainModule = wkBk.VBProject.VBComponents.Item("ThisWorkbook");
workbookMainModule.CodeModule.AddFromString(sCode);