Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Outlook VBA code from c#

In visual studio I am creating an addin, in the addin_startup I am setting an Outlook application to

app = (Microsoft.Office.Interop.Outlook.ApplicationClass)(Marshal.GetActiveObject("Outlook.Application"));

then I am calling a runMacro function which I got from msdn

private void RunMacro(object oApp, object[] oRunArgs)
{
   try
   {

        oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
   }
   catch (Exception e)
   {
        MessageBox.Show(e.GetType().ToString());

    }
}

I pass this function, my Outlook app object and the name of the macro to run in an array, as so...

RunMacro(app, new Object[] { "showFormDisplay" });

I get the following exception

Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
like image 893
tom Avatar asked Nov 19 '25 06:11

tom


1 Answers

I don't know why you have your exact issue, I'd assume one or more parameters is incorrect but not sure since I've never done it the way you're trying to do it.

I'd suggest looking at the following article for a complete sample using somewhat different code, that you might be able to reuse in your code: HOW TO: Run Office Macros by Using Automation from Visual C# .NET

like image 166
Hans Olsson Avatar answered Nov 21 '25 19:11

Hans Olsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!