title: How can I fix "Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable"?
body: <p>I get the above error, which suspends activity in my app, when run on a particular machine. When I run it on my own machine, no such error occurs.</p>
  
  <p>Perhaps "<em>The RPC server is unavailable</em>" is the crux of the problem, but what would cause that to pop up after the app previously working (and still working on my machine)?</p>
  
  <p>The err msg, in more context (showing what seems to be of value/import), is:</p>
  
  <blockquote>
    <p>System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
           at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr&amp; ppTarget, Boolean&amp; pfNeedsRelease)
           at Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType)
           at RoboReporter2017.ExceptionLoggingService.EmailMessageToAssignee(String unit, String notificationRecipient, String rptName)
           at RoboReporter2017.RoboRprtrLib.GenerateAndSaveDueReports()
           at RoboReporter2017.FormMain.RunDueReports()
           at RoboReporter2017.FormMain.FormMain_Load(Object sender, EventArgs e)
        . . .</p>
    
    <p>************** Loaded Assemblies **************
        ----------------------------------------
        Microsoft.Office.Interop.Outlook
            Assembly Version: 12.0.0.0
            Win32 Version: 12.0.4518.1014
            CodeBase: file:///C:/Windows/assembly/GAC/Microsoft.Office.Interop.Outlook/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll
        ----------------------------------------
        office
            Assembly Version: 12.0.0.0
            Win32 Version: 12.0.4518.1014
            CodeBase: file:///C:/Windows/assembly/GAC/office/12.0.0.0__71e9bce111e9429c/office.dll
        ----------------------------------------</p>
  </blockquote>
  
  <p>The method that's breaking on that machine, as referenced in the err msg, is:</p>
  
  <pre><code>internal static void EmailMessageToAssignee(string unit, string notificationRecipient, string rptName)
  {
      string saveLocation = @"\\storageblade\cs\REPORTING\RoboReporter";
      var subject = string.Format("Your {0} report for {1} generated by Robo Reporter 2017", rptName, unit);
      var body = string.Format("Your {0} report for {1} was generated by Robo Reporter 2017 and can be found in the usual location in the shared network folder ({2})", rptName, unit, saveLocation);
  
      Application app = new Application();
      MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
      mailItem.To = notificationRecipient;
      mailItem.Subject = subject;
  
      mailItem.HTMLBody = string.Format(@"&lt;html&gt;&lt;body&gt;&lt;img src='http://www.proactusa.com/bla/images/pa_logo_notag.png' alt='Platypus logo' width='199' height='130' &gt;&lt;p&gt;{0}&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;", body);
  
      mailItem.Importance = OlImportance.olImportanceNormal;
      mailItem.Display(false);
      mailItem.Send();
  }
  </code></pre>
  
  <p>I notice that the Microsoft.Office.Interop.Outlook version in my project's References is 12.0.0.0, the same as the one listed among the "Loaded Assemblies" listed in the err msg.</p>
  
  <h2>UPDATE</h2>
  
  <p>Thinking perhaps that Outlook not running was the problem, I wrote this code:</p>
  
  <pre><code>private static void StartOutlookIfNotRunning()
  {
      string OutlookFilepath = @"C:\Program Files (x86)\Microsoft 
  Office\Office12\OUTLOOK.EXE";
      if (Process.GetProcessesByName("OUTLOOK").Count() &gt; 0) return;
      Process process = new Process();
      process.StartInfo = new ProcessStartInfo(OutlookFilepath);
      process.Start();
  }
  </code></pre>
  
  <p>...adapted from <a href="http://www.kunal-chowdhury.com/2015/11/restart-office-app.html#mAKpUm0c2fkOWEbo.99" rel="nofollow noreferrer">here</a>, but before implementing it, I shut down Outlook and ran the app, to see if I would get that same err msg on my machine if Outlook was not running. But no! It restarts Outlook by itself, without the need for my fancy-pants StartOutlookIfNotRunning() method.</p>
  
  <p>So that's not the problem, anyway...</p>