0

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.

Tyler Montney
  • 1,402
  • 1
  • 17
  • 25

1 Answers1

0

Use Namespace.GetItemFromID. Note the second parameter (store id) is optional. You can omit it if the store in question was already touched by Outlook is in the current session. If not, Outlook will raise the "unknown entry id" exception. If the store entry id is specified, Outlook will open it first, and the store provider will have a chance to register its entry ids with the MAPI system.

You could get item using the below code:

    set App = CreateObject("Outlook.Application")
    set NS = App.GetNamespace("MAPI")
    NS.Logon
    set Msg = NS.GetItemFromID(EntryID)
    MsgBox Msg.Subject

For more information, please refer to these links:

Open Outlook mail Item using EntryID, StoreID, and / or PR_ENTRYID

Alina Li
  • 884
  • 1
  • 6
  • 5
  • Yes, I'm aware CSD doesn't necessarily imply a problem. However, in my case it does (notice my title says "hangs indefinitely"). `GetItemFromID` should not take more than a few seconds (and for me it never completes). If this is not the proper way to fetch an item from the local mailbox, then I'm looking for the proper way. The results I've found online are mixed and often about VBA/Macros. – Tyler Montney Dec 03 '18 at 19:21