none
Need to run/invoke macro from c# code for Outlook Addin RRS feed

  • Question

  • Hi ,

    My requirement demands that i need to execute some macro functions from outlook Addin which is written in  c# code .I was able to do it for all other office products expect for outlook as i don't have access to VBAProject object inside outlook office interloper library .SO please suggest any solution to get rid of this issue .

    Let me know if u need more information.Thanks

    Monday, February 1, 2016 2:01 PM

Answers

  • Hi,

    Finally ,I have made it worked through invoking events Asynchronously.I have invoked selection change event from c # which is define on VBA module and then inside the module i have invoked  mail Item custom property change event which is define in c #.so the cycle went fine for every time when i invoked events .I have define all parameter inside mail item in draft folder  to pass values from c# to VBA and vice verse.

    Tuesday, February 23, 2016 1:32 PM

All replies

  • Hello Sreedhar,

    You can use the late-binding technology for calling VBA macros, see the Type.InvokeMember for more information. For example:

    using Outlook = Microsoft.Office.Interop.Outlook;
    object oMissing = System.Reflection.Missing.Value;
    
    // create outlook object
    Outlook.Application otApp = new Outlook.Application();
    
    string[] functionParameters =
                { 
                    sTo,
                    sCC,
                    sBCC,
                    sSubject,
                    sBody
                };
    
    // Run the macro
    otApp.GetType().InvokeMember("YourVBA",
                System.Reflection.BindingFlags.Default |
                System.Reflection.BindingFlags.InvokeMethod,
                null, otApp, functionParameters);
    
    

    Monday, February 1, 2016 3:15 PM
  • Thanks Eugene for your reply ...

    In place of YourVBA parameter  you mention in InvokerMember method .Do we suppose to pass whole custom  macro function  as string  or its just function function name ? if that is only function name then where should my custom macro function should be saved in order for that function to find it and run .

    Thanks in advance !!!

    Wednesday, February 3, 2016 1:28 PM
  • Hi Sreedhar,

    You have to specify just a macro name. The VBA sub should be defined in the ThisOutlookSession module. See Getting Started with VBA in Outlook 2010 for more information.

    What is the purpose of passing a whole VBA macro to run it? You can do the same operations in your code without macros. Why do you need to call at all?

    Wednesday, February 3, 2016 8:17 PM
  • I have defined my macro function in ThisOutlookSession as below

    Public Function eraseMacro()
    MsgBox "Erasing macro"
    End Function

    And called this code from c sharp outlook 2013 addin

      Globals.ThisAddIn.Application.Application.GetType().InvokeMember("eraseMacro",
                        System.Reflection.BindingFlags.InvokeMethod,
                        null, Globals.ThisAddIn.Application.Application, null);

    and i was getting this error

    System.Runtime.InteropServices.COMException (0x80020006): Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
       at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
       at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)

    -----------------------------------------------------------------------------------------------------------------

    Actually i have to dynamically inject ,run and erase macro functions on outlook using 2013 outlook addin C# code .This for an outlook assessment when i to do this for around 55 questions .If i pre define my macro on ThisOutlookSession  then there is chance for test taker to modify so i have to do it dynamically .

    Friday, February 5, 2016 5:07 PM
  • Hi Sreedhar,

    Based on this thread, the issue seems related to security.

    You may change Programmtic Access in the Trust Center and check the result.  

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 8, 2016 1:45 AM
    Moderator
  • Hi Starain 

    Thanks for your reply but still i see same issue even after changing the Trust Center Settings .

    Wednesday, February 10, 2016 1:45 PM
  • I see the following text in the exception details:

    > (0x80020006): Unknown name.

    This means the code can't locate your VBA macro. Make sure that such macro sub name exists in the default module.

    Wednesday, February 10, 2016 2:20 PM
  • Yes.below was the macro which i defined in default module but still see same error .From my research i found this wont work for outlook 2013 version .

    Public Sub eraseMacro()
    MsgBox "Erasing macro"
    End Sub

    Wednesday, February 10, 2016 8:17 PM
  • Hi Sreedhar,

    Based on my test, I can reproduce that too.

    You may submit the user voice here.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 11, 2016 2:29 AM
    Moderator
  • Hi,

    Finally ,I have made it worked through invoking events Asynchronously.I have invoked selection change event from c # which is define on VBA module and then inside the module i have invoked  mail Item custom property change event which is define in c #.so the cycle went fine for every time when i invoked events .I have define all parameter inside mail item in draft folder  to pass values from c# to VBA and vice verse.

    Tuesday, February 23, 2016 1:32 PM
  • Hi Sreedhar,

    Could you share the detail code here?

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 24, 2016 1:21 AM
    Moderator