Domanda

Stiamo sviluppando Outlook 2007 componente aggiuntivo. Per il test categoria Outlook renaming Ho aggiunto il seguente blocco di codice

 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);

alla fine del componente aggiuntivo metodo private void ThisAddIn_Startup(object sender, EventArgs e). Categoria viene rinominato ma se Outlook è chiuso, le righe precedenti sono commentate, e Outlook viene avviato nuovamente - il nome della categoria non è "TEST !!!" come mi aspettavo. Si tratta di "Gruppo 1" come è stato prima di rinominare. E 'possibile rinominare categoria outlook "per sempre" di codice? Microsoft.Office.Interop.Outlook.Category non ha Save () o Update () o Persistere () metodi.

P.S. Stiamo sviluppando Outlook 2007 aggiuntivo utilizzando Visual Studio 2008, .NET 3.5, C # 3. Il problema è riprodotta con Outlook 2007 SP1 e SP2. Altre versioni di Outlook non sono stati testati.

È stato utile?

Soluzione

Ho risolto il problema (il problema in sé sembra essere Outlook 2007 bug) utilizzando un hack. I seguenti link mi ha aiutato a creare l'hack (oops, non abbastanza reputazione per pubblicare più di 1 link):

L'hack è di per sé dimostrare di seguito:

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
                }
            }
        }
    }
}

Questo metodo di supporto deve essere chiamato prima categoria ridenominazione, per esempio:.

 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);

Altri suggerimenti

Si tratta di un bug di Outlook presenta con Outlook 2007 SP2.

"Si consideri il seguente scenario. Si dispone di un'applicazione personalizzata che può essere eseguito per creare nuove categorie in Outlook 2007. Si esegue l'applicazione per creare una nuova categoria in Outlook 2007. Quindi, se si riavvia Outlook 2007, la categoria che si è creato viene rimosso in modo imprevisto. Questo problema si verifica dopo l'installazione dell'aggiornamento Februarycumulative o SP2. "

C'è un aggiornamento rapido disponibile dal 30 giugno 2009: http://support.microsoft.com/default.aspx/kb/970944/ it

Saluti, Tim

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top