why mailItem.Save() cannot save a category to the first e-mail when i first click on the first e-mail and then click on the second? RRS feed

  • Question

  • I am using c# vsto to develop an add-in which allows to add a new customized category to an e-mail after reading this e-mail.

    The function is working properly if i do like this:

    Select only one e-mail, after read, save the category to the e-mail. The code is like below:

    thisAddin.cs :Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");

    outlookNameSpace.Categories.Add("Read", OlCategoryColor.olCategoryColorGreen, OlCategoryShortcutKey.olCategoryShortcutKeyNone);  

     after read,        mailItem.Categories += "; Read";

    But if i firstly select the new e-mail, and then select the another e-mail, when adding the new category, there comes one error: System.Runtime.InteropServices.COMException msgException.

    So i use "catch" to catch this exception like :

    catch (System.Runtime.InteropServices.COMException msgException)
                            if (msgException.ErrorCode == -2147221239)

    it is working properly and the new category will be added to this e-mail. but when i close outlook, one message box pops up and prompt me "do you want to save the changes", if i click on yes, another message box pop us and says "the item cannot be changed because it was changed by another user or in another window, do you want to make a copy in the deflault folder for the item?". If i continue to click on yes, it will be closed, and the next time i open outlook, there will be the same two e-mails in the folder.

    I want to save the category to this e-mail directly with c# code, so is there any way to avoid the outlook reminding me to save the changes?

    thank you

    • Moved by Rowan Miller Friday, April 22, 2011 6:33 PM Moving to VSTO forum (From:ADO.NET Entity Framework and LINQ to Entities (Pre-Release))
    Friday, April 15, 2011 8:22 AM

All replies