Here's a snippet for interfacing with running Outlook application.
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
Here's my event handler for new messages.
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
Issue is, it hangs on GetItemFromID
. Eventually, I'll get a ContextSwitchDeadlock
exception. Office is 2016 (365 ProPlus, x64, Version 1808). Windows 10 1809. Interop is version 15. Tried running my application under AnyCPU
and x64
.