Condividi tramite


Calling Outlook VBA Macro from outside (VB/VBScript/C#)

It seems to be insignificant but it might turn out to be useful some times.
Consider a situation where you have a VBA Macro in outlook (any office application for that matter) the name of the subroutine is SendCustomEmail and you want it to call from outside the outlook (it should be running or you can make it run).

The sample code to call SendCustomEmail here is in vbscript.

set app= createobject("Outlook.Application")
call app.SendCustomEmail(" test@mydomain.com ")

You will have to write subroutine SendCustomEmail with a parameter in outlook macro.

It is easy in VBScript or VB for that matter as we can directly make use of the late binding.

In case of .Net here in C#

<CODE BLOCK>

Type mtype;
Microsoft.Office.Interop.Outlook.Application outlook;
outlook = new Microsoft.Office.Interop.Outlook.ApplicationClass();
mtype = outlook.GetType();
string[] strArr;
strArr = new string[1];
strArr[0] = "
test@mydomain.com ";
mtype.InvokeMember("SendCustomEmail",System.Reflection.BindingFlags.InvokeMethod,null,outlook,strArr);

</CODE BLOCK>

Similarly we can also call Macro written in word, excel etc.

Hope this turn up to be useful some where.

-Sachin Sancheti
An obstacle is what we see when we take our eyes off the goal.

Comments

  • Anonymous
    May 23, 2007
    I can't get your call to work ... I get 'Object doesn't support this procedure or method'. Any suggestions?

  • Anonymous
    October 06, 2010
    Code has to be in ThisOutlookSession class module to work.

  • Anonymous
    July 08, 2011
    even on ThisOutlookSession  does not work

  • Anonymous
    July 14, 2011
    yeah does not work.......'Object doesn't support this procedure or method'

  • Anonymous
    August 07, 2011
    What an excellent blog it is about VBA .  I also have found valuable information from the site below: www.techyv.com/.../benefits-using-vba-and-vbs-ms-outlook

  • Anonymous
    May 08, 2013
    Doesn't work for me either.  Code is in ThisOutlookSession as a Public Sub.  DOesn't work with or without variables, just get the same message as the other respondants on this page

  • Anonymous
    January 04, 2015
    Doesn't work for OL 2010 but must be ok with OL2003