title: Connecting to an existing Outlook process
body: <p>I'm building an application that opens existing mail messages in Outlook. The user may or may not already be running Outlook. All works well if Outlook is not running, but if it's already running I get a COM error (80080005). The internet seems to indicate that this can happen if the existing Outlook process is running with a higher permission level than the app that tries to bind to it.</p>
  
  <p>Is there some other way for me to ask Outlook to open a message, or do I just need to make sure I match permission levels?</p>
  
  <p>Thanks,</p>
  
  <p>-Patrick</p>
  
  <p><strong>EDIT</strong> Adding code to original question, as Stack Overflow does not permit meaningful formatting in comments.</p>
  
  <p>I was originally doing the following:</p>
  
  <pre><code>var outlook = new Outlook.Application();
  </code></pre>
  
  <p>That line works in all cases except the case where I've launched Outlook prior to launching my application. In that case, I get the aforementioned 80080005 error code.</p>
  
  <p>I've changed this to be a bit more COM-explicit:</p>
  
  <pre><code>Application outlook;
  try
  {
      outlook = (Application)Marshal.GetActiveObject("Outlook.Application");
  }
  catch (COMException ex)
  {
      if (ex.ErrorCode == -2147221021)
          outlook = new ApplicationClass();
      else
          throw;
  }
  </code></pre>
  
  <p>However, that code still does not quite work -- if Outlook is running, I trap an exception whose ErrorCode is 0x800401E3 (MK_E_UNAVAILABLE). But when I attempt to create the new ApplicationClass object, I still get the same 80080005 error code.</p>
  
  <p>I've also tried putting the following into the catch block instead of the <code>new ApplicationClass()</code> line, but there's no difference in behavior:</p>
  
  <pre><code>outlook = (Application) Activator.CreateInstance(
      Type.GetTypeFromProgID("Outlook.Application"));
  </code></pre>