title: Outlook Interop "The RPC server is unavailable" when email is opened
body: <p>Using Outlook Interop, I have created a small app to connect to my Outlook inbox and collect 30 emails, then display them in a grid. When you double click on an email in the grid, it will open the email in Outlook.</p>
  
  <p>In the app:
  - Open an email, minimise it. Then open another, it will open fine.
  - Open an email, close it. Then open another, you will get a 'The RPC server is unavailable. (Exception from HRESULT:
     0x800706BA)' error.</p>
  
  <p>I noticed that when this happens, the Outlook icon in the system tray disappears.</p>
  
  <p>I have tried creating a new instance of the Microsoft.Office.Interop.Outlook.Application and namespace and adding the registry setting found here: <a href="http://blogs.msdn.com/b/rgregg/archive/2008/10/27/application-shutdown-changes-in-outlook-2007-service-pack-2-beta.aspx" rel="nofollow">http://blogs.msdn.com/b/rgregg/archive/2008/10/27/application-shutdown-changes-in-outlook-2007-service-pack-2-beta.aspx</a></p>
  
  <p>I am running Office 2010.</p>
  
  <p>Does anyone know how to get around this?</p>
  
  <pre><code>Imports Microsoft.Office.Interop.Outlook
  Imports System.Runtime.InteropServices
  Imports System.Reflection
  
  Public Class Form1
      Private m_outlookApplication As Application                                                         'Outlook
      Private m_nameSpace As Microsoft.Office.Interop.Outlook.NameSpace                                   'Outlook's namespace
      Private WithEvents m_inboxItems As Items                                                            'All Outlook inbox items
      Private WithEvents m_calendarItems As Items                                                         'All Outlook calendar items
      Private m_outlookInstalled As Boolean = False                                                       'indicates whether Outlook is installed on computer
      Private m_emails As New List(Of OutlookInboxEmail)                                                  'used to store inbox e-mail messages for grid view control
      Private m_inboxFolder As MAPIFolder                                                                 'Outlook inbox folder
      Private m_calendarFolder As MAPIFolder                                                              'Outlook calendar
      Private m_explorer As Explorer                                                                      'Outlook window explorer
      Private m_name As String = String.Empty                                                             'the name user who is connected
      Public Sub New()
          InitializeComponent()
  
          connectToOutlook()
  
          loadInbox()
      End Sub
      Private Sub connectToOutlook()
          m_outlookApplication = New Microsoft.Office.Interop.Outlook.Application
          m_nameSpace = m_outlookApplication.GetNamespace("MAPI")
          m_nameSpace.Logon(Missing.Value, Missing.Value, False, True)
          Dim connectionMode = m_nameSpace.ExchangeConnectionMode
      End Sub
      Private Sub loadInbox()
          Try
  
              'get inbox folder
              m_inboxFolder = m_nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
              'get inbox messages
              m_inboxItems = m_inboxFolder.Items
              m_emails.Clear()
              'display recent messages first
              m_inboxItems.Sort("ReceivedTime", True)
  
              Dim numberOfEmailsToLoad As Integer = 30
              'set displayed values for each message
              For Each currentItem As Object In m_inboxItems
                  Dim emailItem = TryCast(currentItem, MailItem)
                  If Not emailItem Is Nothing Then
                      'check whether its e-mail
                      If emailItem.MessageClass = "IPM.Note" Then
                          'set email
                          Dim inboxEmail As New OutlookInboxEmail
                          inboxEmail.SenderName = emailItem.SenderName
                          inboxEmail.Subject = emailItem.Subject
                          inboxEmail.ReceivedTime = emailItem.ReceivedTime.ToString("dd MMMM HH:mm")
                          inboxEmail.Body = emailItem.Body
                          inboxEmail.Unread = emailItem.UnRead
                          inboxEmail.Email = emailItem
                          m_emails.Add(inboxEmail)
                          numberOfEmailsToLoad = numberOfEmailsToLoad - 1
                          If numberOfEmailsToLoad &lt;= 0 Then
                              Exit For
                          End If
                      End If
                  End If
              Next
  
              If m_explorer Is Nothing Then
                  Try
                      m_explorer = m_outlookApplication.ActiveExplorer
                  Catch ex As System.Exception
  
                  End Try
              End If
  
              If GridControl1.DataSource Is Nothing Then
                  GridControl1.DataSource = Nothing
                  GridControl1.DataSource = m_emails
              End If
              GridControl1.RefreshDataSource()
          Catch exception As System.Exception
  
          End Try
      End Sub
      ''' &lt;summary&gt;
      ''' Opens email in Outlook
      ''' &lt;/summary&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Private Sub openEmail()
          If Not GridView1.GetFocusedDataSourceRowIndex &lt; 0 Then
              Dim selectedEmail = TryCast(m_emails(GridView1.GetFocusedDataSourceRowIndex), OutlookInboxEmail)
              If Not selectedEmail Is Nothing Then
                  Try
  
                      If Process.GetProcessesByName("OUTLOOK").Count() = 0 Then
  
                      End If
  
                      selectedEmail.Email.Display()
                      selectedEmail.Unread = False
                      selectedEmail.EmailImage = My.Resources.Read_16
                  Catch exception As COMException
  
                  End Try
                  GridControl1.RefreshDataSource()
              End If
          End If
      End Sub
      Private Sub GridControlCalendar_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridControl1.DoubleClick
          openEmail()
      End Sub
  End Class
  </code></pre>