Avatar of bjones8888
Flag for United States of America asked on

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

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?

Avatar of undefined
Last Comment

8/22/2022 - Mon

more info...

using Outlook = Microsoft.Office.Interop.Outlook;

... I've also tried

application = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");

Gives the same exact behavior.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy