title: Marshal.GetActiveObject("Outlook.Application") fails while Outlook has focus
body: <p>I am trying to automate Outlook using COM, and we are having terrible problems with Outlook crashing at arbitrary points when trying to use MAPI methods (eg Outlook.Recipient r = MAPI.CreateRecipient("me@there.com")).
  One solution that has been suggested is to use Marshal.GetActiveObject() instead of new Outlook.Application().<br>
  This appears to work fine, however I have run into a very strange issue - the code fires up a copy of outlook and tries to get the application object, but calls to Marshal.GetActiveObject throw a System.Runtime.InteropServices.COMException exception while Outlook is the active application (Has the focus).<br>
  If I run the below code, the try keeps failing over and over.
  However, if I hit ALT-TAB or click anywhere such that Outlook is no longer the active application, the code INSTANTLY succeeds. 
  Any ideas why? Failing that, does anyone have code that will de-focus outlook, so the code succeeds?</p>
  
  <pre><code>   using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Diagnostics;
      using Outlook = Microsoft.Office.Interop.Outlook;
      using System.Threading;
      using System.Reflection;
      using System.Runtime.InteropServices;
  
      namespace MAPI_Repro
      {
          class FakeTest
          {
              public Outlook.Application oApp;
              public Outlook.NameSpace MAPI;
  
              public void DoTest(){
                  if (Process.GetProcessesByName("OUTLOOK").Count() == 0)
                  {
                      var process = Process.Start(new ProcessStartInfo("outlook.exe"));
                  }
                  while (Process.GetProcessesByName("OUTLOOK").Count() == 0)
                  {
                      Thread.Sleep(100);
                  }
  
                  bool success = false;
                  while (!success)
                  {
                      Debug.WriteLine("Waiting for Marshal.GetActiveObject");
                      try
                      {
                          // This FAILS while Outlook is the active application.
                          // As soon as you hit ALT-TAB or click another app, this succeeds.
                          oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                          success = true;
                          Debug.WriteLine("SUCCESS");
                      }
                      catch (System.Runtime.InteropServices.COMException exp)
                      {
                          Debug.WriteLine("FAILED " + exp);
                      }
                      Thread.Sleep(100);
                  }
  
  
                  MAPI = oApp.GetNamespace("MAPI");
                  MAPI.Logon("", "", Missing.Value, Missing.Value);
              }
          }
      }
  </code></pre>