I have an Add-In in Outlook 2010 that saves an email message to a msg file. Included in that is a function called GetApplicationObject( ) that returns a Microsoft.Office.Interop.Outlook object.
In debug everything works as designed. When I run it outside the debugger, just in Outlook itself, the following line of code fails (no error message - it just acts as though there's nothing else to do.
application = Marshal.GetActiveObject("Outlook.Application) as Outlook.Application;
Here's the entire function:
Outlook.Application GetApplicationObject()
{
Outlook.Application application = null;
MessageBox.Show("application set to null");
if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
// If so, use GetActiveObject to cast it to an Application object.
MessageBox.Show("About to get the active Outlook object");
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
// If not, create a new instance of Outlook and log on to the default profile.
MessageBox.Show("No instance found - creating new");
application = new Outlook.Application();
Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
nameSpace.Logon("", "", Missing.Value, Missing.Value);
nameSpace = null;
}
// Return the Outlook Application object.
MessageBox.Show("About to return application object");
return application;
}
When I run in Outlook itself, I see the "application set to null" messagebox, and the "About to get the active Outlook object" messagebox, but not the final one before the return statement. No errors. Nothing.
When I run in debug within VS 2010, it all runs as expected.
Any ideas?
ASKER
using Outlook = Microsoft.Office.Interop.O
... I've also tried
application = (Outlook.Application)Marsh
Gives the same exact behavior.