3

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))
niton
  • 8,771
  • 21
  • 32
  • 52
tom
  • 4,911
  • 11
  • 37
  • 39

3 Answers3

1

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

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • thanks very much for the help. The issue here is that the functions used in the example dont seem to be available for outlook objects, they seem to be bespoke for word, excel access.... (tho theres a very good chance i am wrong ) – tom Nov 12 '10 at 11:50
  • any more ideas, the link you provided just wont work when using access :/ – tom Nov 15 '10 at 10:10
  • 1
    @Tom: I'm afraid not, I've usually just automated Excel and Word, I've not tried any other Office apps very much. – Hans Olsson Nov 15 '10 at 10:27
  • i should have said outlook, not access :$ still the answer remains the same. Its such a pain in the back side - realistically it shouldnt be hard to create an instance of an outlook and then access the associated macros in the .otm file. Unfortunatley its beyond my programming capabilities. Thanks for trying – tom Nov 15 '10 at 15:51
  • Hello. Did you find a solution in the end ? I've the same requirements and can't get it working, even with @user2429879 answer. – May.D Sep 06 '18 at 16:18
0

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.

Enjoy!

0

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!

Falcon
  • 3,150
  • 2
  • 24
  • 35