Search code examples
c#comoutlookoutlook-addincom-interop

Outlook API GetItemFromID() fails with error "Could not open the item.Try again." when using multiple outlook accounts


I am using two outlook accounts from the same exchange server (i.e. same domain). We have an outlook add-in which we use to archive some emails from outlook to an external web application. While composing a new mail, it's possible to attach some files from external application to the mail being composed using the add-in. Now things work perfectly when I have a single account on outlook. But after configuring another account and trying to do the same from secondary account, outlook gives error:

Outlook Initiator exception: System.Runtime.InteropServices.COMException (0x80040107): Could not open the item. Try again. at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetItemFromID(String EntryIDItem, Object EntryIDStore)

I tried to dig into the code and found that the call to GetItemFromID() gives the exception shown above.

object item = ns.GetItemFromID(objectID, storeId);

The first parameter objectID (which is EntryIDItem) is different for different accounts but second parameter storeId is same.

I also tried to see the body of the method GetItemFromID() using disassembler but the method is defined as an extern method that means it's defined outside of the current assembly as un-managed code.

Tried to search for anything similar to that on stackoverflow but no luck. Can anybody please help me on this?


Solution

  • Try to leave only the first parameter. The EntryIDStore parameter is optional. See NameSpace.GetItemFromID for more information.

    Note, the Entry ID changes when an item is moved into another store, for example, from your Inbox to a Microsoft Exchange Server public folder, or from one Personal Folders (.pst) file to another .pst file. Solutions should not depend on the EntryID property to be unique unless items will not be moved.