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))


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


I thik you're doing it right, but maybe you don't meet the security requirements! In order to run a macro, the office file needs to be a trusted source! It must be flagged via the Office Security Center or else you won't be able to execute macros. You also need to allow access to the VBA Object via the security center for an external app to call the macros!


I had the same problem, the article from Microsoft (HOW TO: Run Office Macros by Using Automation from Visual C#) doesn't help because it covers only calling Outlook macros from Word, Excel, and Access.

Now I finally found the solution on a French site! It covers sending from Python but works also with C#! French Python code: http://www.developpez.net/forums/d1239845/autres-langages/python-zope/bibliotheques-tierces/pywin32-appeler-macro-outlook/

So I ported it to C# with the function I need (where "FnSendMailSafe" is the macro name in Outlook 2007.

using Outlook = Microsoft.Office.Interop.Outlook; object oMissing = System.Reflection.Missing.Value; // create outlook object Outlook.Application otApp = new Outlook.Application(); string[] arFunctionParameters = { sTo, sCC, sBCC, sSubject, sBody }; // Run the macro otApp.GetType().InvokeMember("FnSendMailSafe", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, otApp, arFunctionParameters); System.Runtime.InteropServices.Marshal.ReleaseComObject (otApp); otApp = null; GC.Collect(); //Garbage collection.



  • Error 2147463168 when trying to bind to an AD User Object
  • StreamSocket, DataWriter.StoreAsync(), DataReader.LoadAsync() — Asynchronous problems
  • The type initializer for 'Emgu.CV.CvInvoke' threw an exception for Win8 64bit, VS2012
  • File not found when registering DLL with TFileRun and regsvr32
  • logout not working after SeqReset
  • Unable to load Win32 Native DLL file from C#.NET
  • How to convert amr files to mp3 using C#
  • C# Outlook - Call Rejected By Callee
  • What causes CORDBG_E_CLASS_NOT_LOADED (HRESULT: 0x80131303)
  • COM - [in] parameter as SAFEARRAY(STRUCT)
  • Call Outlook VBA code from c#
  • How to provide row updates to table in Azure SQL DW using SSDT?
  • Office 365 API throwing 500 on /me/calendars endpoint
  • Azure Functions 1.0.7 giving Could not load file or assembly error with EF Core 2
  • Error: identifier :“SHGetKnownFolderPath” is unidentified
  • Is there a way with MIDL to turn off C-style headers generation?
  • Getting ExitCode From Exception Handler
  • SQLite UWP Error with Mobile EMulator Windows 10
  • Remove an item from a list box causes a Catastrophic Failure?
  • numpy 64bit support in PTVS and numpy System.Int64 casting
  • phpmailer - How to verify a sent email arrived at its destination
  • Java: java.util.ConcurrentModificationException
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • Appium MobileElement swipe returns unknown server error
  • Connecting bluetooth device to windows phone 8 application
  • Building Qt project for C++11 standard
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • Trying to get the char code of ENTER key
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • javascript inside java/jsp code
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Android Studio and gradle
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • How can i traverse a binary tree from right to left in java?