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

  1. tsawyer@yorkcounty.gov says:

    I can’t get your call to work … I get ‘Object doesn’t support this procedure or method’.

    Any suggestions?

  2. James says:

    Code has to be in ThisOutlookSession class module to work.

  3. No says:

    even on ThisOutlookSession  does not work

  4. me says:

    yeah does not work…….'Object doesn't support this procedure or method'

  5. Jackson says:

    What an excellent blog it is about VBA .  I also have found valuable information from the site below:

    http://www.techyv.com/…/benefits-using-vba-and-vbs-ms-outlook

  6. Henry says:

    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

  7. Oliv- says:

    Doesn't work for OL 2010 but must be ok with OL2003