views:

419

answers:

2

We are developing Outlook 2007 add-in. For testing outlook category renaming I've added the following code block

 var session = Application.Session;
 var categories = session.Categories;
 var category1 = session.Categories[1];

 //catefory1.Name is "Group1" before executing line below
 category1.Name = "TEST!!!";

 Marshal.ReleaseComObject(category1);
 Marshal.ReleaseComObject(categories);
 Marshal.ReleaseComObject(session);

to the end of add-in private void ThisAddIn_Startup(object sender, EventArgs e) method. Category is renamed but if Outlook is closed, the above lines are commented, and outlook is started again - the category name is not "TEST!!!" as I expected. It is "Group1" as is was before renaming. Is it possible to rename outlook category "forever" by code? Microsoft.Office.Interop.Outlook.Category has no Save() or Update() or Persist() methods.

P.S. We are developing Outlook 2007 add-in using Visual Studio 2008, .net 3.5, C# 3. The problem is reproduced with Outlook 2007 SP1 and SP2. Other outlook versions were not tested.

A: 

I have solved the problem (the problem itself seems to be Outlook 2007 bug) using a hack. The following links helped me to create the hack (oops, not enough reputation to post more then 1 link):

The hack itself is show below:

using System;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Office.Interop.Outlook;

namespace OutlookHack
{
    public static class OutlookCategoryHelper
    {
        private const string CategoryListStorageItemIdentifier = "IPM.Configuration.CategoryList";
        private const string CategoryListPropertySchemaName = @"http://schemas.microsoft.com/mapi/proptag/0x7C080102";
        private const string CategoriesXmlElementNamespace = "CategoryList.xsd";
        private const string XmlNamespaceAttribute = "xmlns";
        private const string CategoryElement = "category";
        private const string NameAttribute = "name";

        public static void RenameCategory(string oldName, string newName, Application outlookApplication)
        {
            MAPIFolder calendarFolder = outlookApplication.Session.GetDefaultFolder(
                OlDefaultFolders.olFolderCalendar);
            StorageItem categoryListStorageItem = calendarFolder.GetStorage(
                CategoryListStorageItemIdentifier, OlStorageIdentifierType.olIdentifyByMessageClass);

            if (categoryListStorageItem != null)
            {
                PropertyAccessor categoryListPropertyAccessor = categoryListStorageItem.PropertyAccessor;
                string schemaName = CategoryListPropertySchemaName;
                try
                {
                    // next statement raises Out of Memory error if property is too big
                    var xmlBytes = (byte[])categoryListPropertyAccessor.GetProperty(schemaName);

                    // the byte array has to be translated into a string and then the XML has to be parsed
                    var xmlReader = XmlReader.Create(new StringReader(Encoding.UTF8.GetString(xmlBytes)));

                    // xmlWriter will write new category list xml with renamed category
                    XmlWriterSettings settings = new XmlWriterSettings { Indent = true, IndentChars = ("\t") };
                    var stringWriter = new StringWriter();
                    var xmlWriter = XmlWriter.Create(stringWriter, settings);

                    xmlReader.Read(); // read xml declaration
                    xmlWriter.WriteNode(xmlReader, true);
                    xmlReader.Read(); // read categories
                    xmlWriter.WriteStartElement(xmlReader.Name, CategoriesXmlElementNamespace);
                    while (xmlReader.MoveToNextAttribute())
                    {
                        if (xmlReader.Name != XmlNamespaceAttribute) // skip namespace attr
                        {
                            xmlWriter.WriteAttributeString(xmlReader.Name, xmlReader.Value);
                        }
                    }
                    while (xmlReader.Read())
                    {
                        switch (xmlReader.NodeType)
                        {
                            case XmlNodeType.Element: // read category
                                xmlWriter.WriteStartElement(CategoryElement);
                                while (xmlReader.MoveToNextAttribute())
                                {
                                    if ((xmlReader.Name == NameAttribute) && (xmlReader.Value == oldName))
                                    {
                                        xmlWriter.WriteAttributeString(NameAttribute, newName);
                                    }
                                    else
                                    {
                                        xmlWriter.WriteAttributeString(xmlReader.Name, xmlReader.Value);
                                    }
                                }
                                xmlWriter.WriteEndElement();
                                break;
                            case XmlNodeType.EndElement: // categories ended
                                xmlWriter.WriteEndElement();
                                break;
                        }
                    }
                    xmlReader.Close();
                    xmlWriter.Close();

                    xmlBytes = Encoding.UTF8.GetBytes(stringWriter.ToString());
                    categoryListPropertyAccessor.SetProperty(schemaName, xmlBytes);
                    categoryListStorageItem.Save();
                }
                catch (OutOfMemoryException)
                {
                    // if error is "out of memory error" then the XML blob was too big
                }
            }
        }
    }
}

This helper method must be called prior to category renaming, e.g.:

 var session = Application.Session;
 var categories = session.Categories;
 var category1 = session.Categories[1];

 //catefory1.Name is "Group1" before executing line below
 OutlookCategoryHelper.RenameCategory(category1.Name, "TEST!!!", Application);
 category1.Name = "TEST!!!";

 Marshal.ReleaseComObject(category1);
 Marshal.ReleaseComObject(categories);
 Marshal.ReleaseComObject(session);
Kluyg
http://www.outlookcode.com/codedetail.aspx?id=1583 - almost the same code, but in VBA + Redemption library (200$ for commercial use). Getting category list using redemption will not throw OutOfMemoryException.
Kluyg
A: 

This is an Outlook bug introduces with Outlook 2007 SP2.

"Consider the following scenario. You have a custom application that can be run to create new categories in Outlook 2007. You run the application to create a new category in Outlook 2007. Then, if you restart Outlook 2007, the category that you created is removed unexpectedly. This problem occurs after you install the Februarycumulative update or SP2."

There is a hotfix available since June 30, 2009: http://support.microsoft.com/default.aspx/kb/970944/en

Regards, Tim

Tim Schmelter
Tim, unfortunately, this is related, but not the same bug. Bug with category renaming is not SP2-only, as I have mentioned earlier in my original question. And Remou wrote the same that you (see comments to my original question) 1 month ago. Bug with category creation is different, it is SP2-only, it has hotfix. And the hotfix does not solve bug with renaming.
Kluyg