none
Same COMException with different ErrorCode RRS feed

  • Question

  • I have discovered a strange behaviour in my code regarding COMException and its error code.

    I have two solutions with a Word VSTO Project called MyWordAddinV1 and MyWordAddinV2. Both add ins are nearly the same. One Piece of code is exactly the same and looks like:

    Outlook.Application outlook = GetOrStartOutlook(); Outlook.Folder senderFolder = GetSenderFolder(outlook.Session); var sender = senderFolder.Items.Add("IPM.Contact.Sender") as Outlook.ContactItem; var inspector = outlook.Inspectors.Add(sender); inspector.Display(true); try { // Users can delete the contact within the inspector window. So lets perform a check. string id = sender.EntryID; } catch (System.Runtime.InteropServices.COMException ex) { // Error: The item has been moved or deleted if (ex.ErrorCode == unchecked((int)0x8A44010A)) { return null; } throw; }

    return MapToSender(sender);


    Both add ins throw a COMException with the same error message ("The item has been moved or deleted"). However the error code within one add in is 0x8A44010A and 0x8C54010A within the other add in.

    Edit: This gets more strange. When running the code multiple times the error code changes each time. Always in the predictable same order after restart. For V1 the COMException has the error codes: 0x8A44010A, 0x9074010A, 0x96A4010A, 0x9CD4010A, ... For V2 the COMException has the error codes: 0x8C54010A, 0x9284010A, 0x98B4010A, 0x9EE4010A, ...

    Does someone know why this happens? I would have expected that the error codes in both add ins for multiple runs are the same.

    • Edited by Yavuz T Monday, April 13, 2015 4:33 PM
    Monday, April 13, 2015 4:06 PM

Answers

All replies

  • Yavuz,

    Typically when you need to continue running your code against the displayed item you can get the item using the ActiveInspector method of the Application class or handle events that the Outlook object model provides.

    I suppose the error code depends on the store implementation. Nobody can guarantee that you get the single error code. 

    Monday, April 13, 2015 7:14 PM
  • Hi Eugene,

    Thank You for replying. Based on part of your response I have the feeling that you have not clearly understand what happens.

    Typically when you need to continue running your code against the displayed item you can get the item using the ActiveInspector method of the Application class or handle events that the Outlook object model provides.

    So let me clarify it for you. The user clicks in Word a WindowsForms button which then runs the above code. A contact item will be created and opened in a modal inspector window. The user can fill out the form and save the item or delete it. The code will be executed after one of the actions occur. From the perspective of Outlook it does not matter whether the user has clicked on Save or Delete but it does matter for the above code which runs in Word.

    If your assumption

    I suppose the error code depends on the store implementation. Nobody can guarantee that you get the single error code. 

    is correct then the VSTO programming model is a bad programming model. It is always the same error (accessing an item which was moved or deleted) so it should return always the same error code.

    However I found a workaround to detect from Word if the user has clicked the delete button.

    Outlook.Application outlook = GetOrStartOutlook();
    Outlook.Folder senderFolder = GetSenderFolder(outlook.Session);
    var sender = senderFolder.Items.Add("IPM.Contact.Sender") as Outlook.ContactItem;
    sender.Save();
    string senderEntryID = sender.EntryID;
    
    var inspector = outlook.Inspectors.Add(sender);
    inspector.Display(true);
    
    sender = this.outlook.Session.GetItemFromID(senderEntryID);
    var folder = sender.Parent as Outlook.Folder;
    
    Outlook.Folder deletedFolder = outlook.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems) as Outlook.Folder;
    if(deletedFolder.EntryID == folder.EntryID)
    {
        return null;
    }
    
    return MapToSender(sender);

    Tuesday, April 14, 2015 11:51 AM
  • VSTO and store providers are entirely different things.

    Your solution is based on my previous post. But it requires saving the item first.

    If you need to know exactly whether the Delete button was clicked by the user you need to repurpose ribbon controls. See Temporarily Repurpose Commands on the Office Fluent Ribbon .

    Tuesday, April 14, 2015 12:43 PM