title: C# Get running Outlook instance in VSTO add-in
body: <p>I am trying to get an Outlook Application object in my add-in for Excel.</p>
  
  <p>If there's a running Outlook instance, it should get that, if there isn't any, it should create one, using the Outlook object model.</p>
  
  <p>This is the code I have right now:</p>
  
  <pre><code>public static Outlook.Application GetApplicationObject()
  {
      Outlook.Application application = null;
  
      if (Process.GetProcessesByName("OUTLOOK").Count() &gt; 0)
      {
          application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
      }
      else
      {
          application = new Outlook.Application();
      }
      return application;
  }
  </code></pre>
  
  <p>My problem: it finds Outlook processes, but can't get them, throwing the following error message: </p>
  
  <blockquote>
    <p>Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))</p>
  </blockquote>
  
  <p>I tried debugging it step by step, and monitored the task manager. I could see that I have an Outlook instance, but it's only an icon in the right side of the taskbar. Does this mean, that the instance is not fully loaded yet, and it can't be accessed, to get the Application object from it?</p>
  
  <p>I ended up modifying my code, and separating the if-else into 2 try-catches, with their own returns, but I still think that the code above should be usable.</p>