1
votes

I am using C# to build an Excel AddIn by VSTO, one thing I want to do is automatically import a VBA module which I wrote(a .bas file ) into Excel and run it when the user importing my Excel AddIn from Com AddIns in Excel.

I tried like:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.Run(@"C:\myModule.bas");
    }

But got errors: Cannot run the macro 'C:\myModule.bas'. The macro may not be available in this workbook or all macros may be disabled.

Thanks in advance to people who read my question!

1

1 Answers

4
votes

It is as simple as

wb.VBProject.VBComponents.Import ("C:\\Module1.Bas");

You do not run a module. You run a procedure in the module. To run the procedure, simply use

oXL.Run "Procedure Name(parameters if any)"

Read up more How to dynamically add and run a VBA macro from Visual Basic. It is explained in VBA but I am sure you can easily adapt it to C#. Let me know if you have any issues.