Reputation: 51
I have made an VBA Macro which gets all the events from an Outlook calendar and writes them to an excel file.
I then need to execute this macro from either a powershell script, or an C# console app.
Is there any way to do this?
Im on win10 using Outlook 2013.
Upvotes: 0
Views: 1746
Reputation: 71217
Outlook's Application
class doesn't have a Run
member, and makes it extremely hard to invoke VBA code out-of-process (heck, even in-process), presumably for security reasons. When Rubberduck initially implemented unit testing support for Outlook VBA, it was a literal hackjob involving creating a commandbar button and assigning its "OnAction" to the unit test procedure we wanted to invoke - like this:
var app = Application;
var exp = app.ActiveExplorer();
CommandBar cb = exp.CommandBars.Add("RubberduckCallbackProxy", Temporary: true);
CommandBarControl btn = cb.Controls.Add(MsoControlType.msoControlButton, 1);
btn.OnAction = declaration.QualifiedName.ToString();
btn.Execute();
cb.Delete();
So if you make btn.OnAction = "YourMacro";
and have a reference to the Outlook interop assembly (Application
is Microsoft.Office.Interop.Outlook.Application
), I believe this can work - the macro must be in a standard module though - if it's in ThisOutlookSession
then the wheels come off.
(note, since then we've figured out a much more elegant, host-agnostic way to run the user's test methods, but that wouldn't work out-of-process)
The ThisOutlookSession
object is extended at run-time with the public members of that class, so if your macro is written in ThisOutlookSession
then from VBA code you can invoke it like app.MacroName
, but we haven't been able to get that to work from C# code.
Lastly, note that the above code will be leaking COM objects. Make sure you clean up unmanaged objects properly, otherwise you'll see a ghost OUTLOOK.EXE process lingering in the task manager waiting to be killed, well afer your script/console app is gone.
Upvotes: 2