1

下面是创建 Module1.bas 并在其上存储代码。我想添加以下代码以从“Thisworkbook”触发宏。

如何在 Thisworkbook 中添加 Workbook_Open() ?

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);
4

1 回答 1

0
var workbookMainModule = wkBk.VBProject.VBComponents.Item("ThisWorkbook");
    workbookMainModule.CodeModule.AddFromString(sCode);
于 2012-08-22T08:31:33.913 回答