Avatar of bjones8888
bjones8888Flag 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;
            }
            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?
OutlookGroupwareC#

Avatar of undefined
Last Comment
bjones8888
Avatar of bjones8888
bjones8888
Flag of United States of America image

ASKER

more info...

using Outlook = Microsoft.Office.Interop.Outlook;

... I've also tried

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

Gives the same exact behavior.
ASKER CERTIFIED SOLUTION
Avatar of bjones8888
bjones8888
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Outlook
Outlook

Microsoft Outlook is a personal information manager from Microsoft, available as a part of the Microsoft Office suite. Although often used mainly as an email application, it also includes a calendar, task manager, contact manager, note-taker, journal, and web browser.

105K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo