Call Outlook VBA code from c#

后端 未结 3 996
野性不改
野性不改 2021-01-20 19:37

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

app = (Microsoft.Office.Interop.Outlook.ApplicationClas         


        
3条回答
  •  无人及你
    2021-01-20 20:18

    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!

提交回复
热议问题