none
Error when using 2 types of reference to same Outlook.MailItem object C# RRS feed

  • Question

  • Hi All,

    I've noticed weird behavior in Outlook when referencing to the same Outlook.MailItem object in C#.

    We use 2 ways to reference to the object:

    1. Using the EntryID to get the object using *.GetItemFromEntryID

    2. Using e.g. Inspector.CurrentItem, so direct reference to the object

    When I use method 1 to get the object and add userproperties to it and then save the object. Any reference to the same mailitem using method 2 becomes unusable. I'm no longer able to save the mailitem.

    Can someone tell me why this is happening?

    How to prevent and/or fix it?

    Best regards,

    Jouke van Rossum


    Tuesday, May 22, 2018 7:07 AM

All replies

  • Hello Jouke,

    Why do you need to use both ways of accessing Outlook objects?

    The GetItemFromID method is used to get an Outlook item instance if there is no other way to get an object, for example, when you execute a deferred action. Or just get a string as a parameter (NewMailEx). Use the Inspector.CurrentItem property where possible.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, May 22, 2018 9:42 AM
  • Hi Eugene,

    I'm not using both methods... 2 add-ins are both talking to the same message and I use the GetItemFromEntryID function to work with the message, but the other add-in is using the direct reference to Inspector.CurrentItem.

    I would like to know why we cannot use both methods!?

    Why is using both methods on the same message at the same time raising the "The operation cannot be perfomed,  message has been changed." error.

    Tuesday, May 22, 2018 12:32 PM
  • Hi Jouke,

    Could you share us a solution project which could reproduce your issue?

    I made a test with steps below, but, i fail to reproduce your issue.

    1. Create first VSTO AddIn with ribbon button below:

            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                MailItem mail = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                mail.Subject = "Inspector Subject";
                mail.Save();
            }

    2. Create second VSTO AddIn with ribbon below:

            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                MailItem mail = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                MailItem mailItem = Globals.ThisAddIn.Application.Session.GetItemFromID(mail.EntryID);
                mailItem.Subject = "EntryID Subject";
                mailItem.Save();
            }
    

    3. Run the projects, and click the buttons for the same mail item.

    Is there any different between you scenario?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 23, 2018 8:03 AM
  • Hi Tao Zhou,

    Below you can find a VBA snippet which reproduces the same error:

    The code

    Set oMail = Inspector.CurrentItem

    Dim sEntryID As String

    sEntryID = oMail.EntryID

    If (sEntryID = "") Then Exit Sub

      Dim olNS As Outlook.NameSpace

    Set olNS = Application.GetNamespace ("MAPI")

    Set oMailEntryID = olNS.GetItemFromID (sEntryID)

    Explanation of issue

    Now there are two references to the same mail item.

    As soon as the object "oMailEntryID" invokes save, the objects that are a direct reference to Inspector.CurrentItem become unusable.

    If two applications work with a direct reference and if things are changed and saved independently of each other, there is no problem.

    Best regards, Jouke


    Wednesday, May 23, 2018 2:03 PM
  • Hi Jouke,

    Could you share us the complete steps to reproduce your issue?

    I made a test with code below, but fail to reproduce.

    Sub ReferenceTest()
    Set omail = ActiveInspector.CurrentItem
    
    Dim sEntryID As String
    
    sEntryID = omail.EntryID
    
    If (sEntryID = "") Then Exit Sub
    
      Dim olNS As Outlook.NameSpace
    
    Set olNS = Application.GetNamespace("MAPI")
    
    Set oMailEntryID = olNS.GetItemFromID(sEntryID)
    oMailEntryID.Save
    Debug.Print omail.Subject
    End Sub

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 24, 2018 6:42 AM
  • Hi Tao,

    Unfortunatly I'm unable to create a new VSTO which can produce the same error... (I keep trying though)

    I currently have my project in which the error is raised, is there another way I could show you the code or error?

    Best regards,

    Jouke van Rossum

    Monday, May 28, 2018 7:29 AM
  • I made a test with above VBA code which you said could reproduce your issue, but it works at my side.

    Could you share us how to reproduce your issue with these VBA code?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 29, 2018 7:15 AM