troubleshooting Question

Marshal.GetActiveObject("Outlook.Application") - only working in debug

Avatar of bjones8888
bjones8888Flag for United States of America asked on
2 Comments1 Solution4649 ViewsLast Modified:
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;
                // 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?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros