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

bjones8888
bjones8888 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
bjones8888President

Author

Commented:
more info...

using Outlook = Microsoft.Office.Interop.Outlook;

... I've also tried

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

Gives the same exact behavior.
President
Commented:
I found the solution.

1. I reverted the statement back to the original:
   application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;

2. In the solution's Configuration Manager, set the individual projects configuration to Release instead of Debug.
 
3. In the Setup project's File System on Target Machine (Right click solution -> View | File System -> Application Folder.  Removed the .manifest and the .vsto files (they had been taken from bin\Debug -- then added them from bin\Release.

Rebuild and then right click the setup project -> Install.  

Worked perfectly!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial