Skip to content

How to execute from C# a VBA macro in an open Excel workbook?

Guest User

Asked Aug 9, 2007, 6:14 AM EDT1 answer
Share
LinkedInX

I have a C# executable that opens an existing Excel workbook as follows:

string workbookPath = "c:/SomeWorkBook.xls";

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,

0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",

true, false, 0, true, false, false);

This works fine and I can read from and write data to cells on various worksheets in that workbook.

One of the worksheets, say SHEET9, has associated with it (i.e. if you open the VBA Editor, navigate to the worksheet, you see the routine) a VBA routine that does some calculations.  I want to call this routine from the C# code.

Suppose the VBA routine looks like:

Private Sub Run_BLAH_Click()

  Call PREP_OUTPUT

  Call SIMEXECUTE

End Sub

How do it call this Run_BLAH_Click routine on SHEET9 from C# after that workbook has been opened?  Is it a problem that the VBA routine is on the sheet rather than in a module?


Thanks in advance.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    I found a knowledge base article on it.  I suspect you do it the same way in C#.   (Not the ideal solution, but I suspect Microsoft doesn't make it easy due to security)

    http://support.microsoft.com/kb/194611

    0