Changed MessageClass of unread mail item in "Unread Mail" folder cannot be saved without error

220 Views Asked by At

I've got a custom Outlook 2013+ add-in using Add-In Express with a button on the main Ribbon that when clicked changes the message class of the selected mail item (amongst other actually-useful operations).

This works fine in all situations except for if the currently selected folder in Outlook is the Unread Mail folder. On saving the MailItem with the updated MessageClass for unread items in this folder I get the error:

The operation cannot be performed because the message has been changed.

This also occurs if the item becomes read automatically via the Reading Pane setting, but not if the message is manually marked as read before the button is clicked. It does not occur if other less fundamental properties (like the Subject) of the mail item are amended in the same way.

The following example illustrates the issue (bounds checks/exception handling omitted for brevity):

public void ChangeSelectedMailItemMessageClass()
{
   var activeExplorer = _outlookApp.ActiveExplorer();
   var selection = activeExplorer.Selection;
   var item = selection[1] as MailItem;

   item.MessageClass = "Some.Valid.Message.Class";
   item.Save(); // 'The operation cannot be performed because the message has been changed.'

   Plugin.Marshal.ReleaseComObject(item);
   Plugin.Marshal.ReleaseComObject(selection);
   Plugin.Marshal.ReleaseComObject(activeExplorer);
}

The same behaviour can be seen trying to set custom user properties for items in a search folder whose criteria references that user property, so I'm suspecting that it's related to search folders rather than the Unread Mail folder specifically.

Releasing the selection and activeExplorer references before amending the MailItem makes no difference, and the lifetime of the MailItem from read to amend is as small as it can be made (given the above snippet).

Maybe pertinent info:

  • Outlook 2016 though replicates on 2013
  • Using Add-In Express though suspect unrelated
  • COM objects being released as soon as possible, as far as I can tell
  • Outlook connected to Exchange
  • Online vs Offline mode makes no difference, the error is the same
  • The messages are in my own inbox or some subfolder, no other mailboxes connected to Outlook at the time

Is it possible to amend the MessageClass of an unread item from the Unread Mail folder without encountering an error?

Edit: Appears that another installed add-in is interfering somehow in the process, but it's not clear how or if my add-in can be coded in such a way as to not break on the back of another add-in getting creative with the selected MailItem

1

There are 1 best solutions below

0
On

Seems that another installed add-in is somehow interfering with the process of changing the MessageClass in this instance, and that it's an incompatibility rather than a fundamental issue with amending the MessageClass of an item in the Unread Mail folder.

Of course, replicating the issue with no other add-ins active should have been the first port of call!