这里有一个与运行的Outlook应用程序接口的片段。
Try
OutlookObj = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application")
Catch ex As Exception
Console.WriteLine("Something went wrong while trying to connect to Outlook. Make sure Outlook is running. Press any key to exit.")
Console.Read()
Exit Sub
End Try
OutlookInspectors = OutlookObj.Inspectors
AddHandler OutlookObj.NewMailEx, AddressOf OutlookObj_NewMail
这是我的新消息事件处理程序。
Private Sub OutlookObj_NewMail(ByVal ID As String)
Dim Item As Microsoft.Office.Interop.Outlook.MailItem = OutlookObj.Application.Session.GetItemFromID(ID)
'Further processing...
End Sub
问题是,它挂在GetItemFromID
上。最终,我会得到一个ContextSwitchDeadlock
异常。Office是2016(365 ProPlus,x64,版本1808(。Windows 10 1809。Interop是版本15。尝试在AnyCPU
和x64
下运行我的应用程序。
使用Namespace.GetItemFromID。请注意,第二个参数(存储id(是可选的。如果Outlook已在当前会话中接触到有问题的存储,则可以省略它。否则,Outlook将引发"未知条目id"异常。如果指定了存储条目id,Outlook将首先打开它,存储提供程序将有机会向MAPI系统注册其条目id。
您可以使用以下代码获得物品:
set App = CreateObject("Outlook.Application")
set NS = App.GetNamespace("MAPI")
NS.Logon
set Msg = NS.GetItemFromID(EntryID)
MsgBox Msg.Subject
有关更多信息,请参阅以下链接:
使用EntryID、StoreID和/或PR_EntryID 打开Outlook邮件项目