经过几个小时的调查,当一封邮件被关闭时,它会随之关闭Outlook。
解决方案是在与Outlook交互之前检查Outlook是否已打开。计时器每2分钟运行一次,并运行相同的检查,确保我创建的Outlook收件箱视图在Outlook关闭时只过期2分钟。
Private Sub openEmail()
If Not GridViewInbox.GetFocusedDataSourceRowIndex < 0 Then
Dim selectedEmail = TryCast(m_emails(GridViewInbox.GetFocusedDataSourceRowIndex), OutlookInboxEmail)
If Not selectedEmail Is Nothing Then
Try
If Process.GetProcessesByName("OUTLOOK").Count() = 0 Then
'if outlook is not open.. open it
m_selectedEmail = selectedEmail
reconnectOutlook()
Else
'as outlook is open, open the email
selectedEmail.Email.Display()
selectedEmail.Unread = False
selectedEmail.EmailImage = ImageLibrary.My.Resources.Read_16
GridControlInbox.RefreshDataSource()
m_selectedEmail = Nothing
End If
Catch exception As COMException
EventLog.write(exception)
MessageBox.Show("Could not open email. Please start Outlook and try again.", "Opening Email", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
GridControlInbox.RefreshDataSource()
End If
End If
End Sub
重新连接代码。。。
Private Sub connectToOutlook()
Try
m_outlookApplication = New Microsoft.Office.Interop.Outlook.Application
m_nameSpace = m_outlookApplication.GetNamespace("MAPI")
'start Outlook
Dim connectionMode = m_nameSpace.ExchangeConnectionMode
m_nameSpace.Logon(Missing.Value, Missing.Value, False, True)
'get the name of user
m_name = m_outlookApplication.Session.CurrentUser.Name
m_outlookInstalled = True
Catch exception As System.Exception
If Not exception.Message.IndexOf("Class not registered") = -1 Then
'Outlook is not installed
m_outlookInstalled = False
EventLog.write(exception)
Else
EventLog.write(exception)
End If
End Try
End Sub