Bala Kumar
Bala Kumar

Reputation: 667

How can i Access "ThisWorkbook " of excel in c#

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

Answers (1)

Bala Kumar
Bala Kumar

Reputation: 667

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

Upvotes: 0

Related Questions