如何在c#中访问excel的“ThisWorkbook”

时间:2012-08-21 12:48:31

标签: excel-vba excel-2007 excel vba

创建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);

1 个答案:

答案 0 :(得分:0)

var workbookMainModule = wkBk.VBProject.VBComponents.Item("ThisWorkbook");
    workbookMainModule.CodeModule.AddFromString(sCode);