문제

우리는 Outlook 2007 애드 인을 개발하고 있습니다. Outlook 카테고리 테스트를 위해 다음 코드 블록을 추가했습니다.

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

애드 인의 끝까지 private void ThisAddIn_Startup(object sender, EventArgs e) 방법. 카테고리가 바뀌었지만 Outlook이 닫히면 위의 줄이 주석을 달고 Outlook이 다시 시작됩니다. 카테고리 이름은 "테스트 !!!"가 아닙니다. 내가 예상 한대로. 이름을 변경하기 전과 마찬가지로 "Group1"입니다. 코드별로 Outlook 범주 "Forever"이름을 바꿀 수 있습니까? Microsoft.office.interop.outlook.category에는 save () 또는 update () 또는 persist () 메소드가 없습니다.

PS는 Visual Studio 2008, .NET 3.5, C# 3을 사용하여 Outlook 2007 애드 인을 개발하고 있습니다. 문제는 Outlook 2007 SP1 및 SP2로 재현됩니다. 다른 Outlook 버전은 테스트되지 않았습니다.

도움이 되었습니까?

해결책

해킹을 사용하여 문제를 해결했습니다 (문제 자체는 Outlook 2007 버그 인 것 같습니다). 다음 링크를 사용하면 해킹을 만드는 데 도움이되었습니다 (OOPS, 1 링크 이상을 게시 할 수있는 평판이 충분하지 않음).

핵 자체는 아래에 표시됩니다.

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

이 헬퍼 방법은 카테고리 변경 전에 호출되어야합니다.

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

다른 팁

이것은 Outlook 2007 SP2와 함께 소개 된 Outlook 버그입니다.

"다음 시나리오를 고려하십시오. Outlook 2007에서 새로운 카테고리를 만들기 위해 실행할 수있는 사용자 정의 응용 프로그램이 있습니다. Outlook 2007에서 새로운 카테고리를 만들기 위해 응용 프로그램을 실행합니다. 그러면 Outlook 2007을 다시 시작하면 생성 한 카테고리는 IS입니다. 예기치 않게 제거되었습니다.이 문제는 Februbercumulative Update 또는 SP2를 설치 한 후에 발생합니다. "

2009 년 6 월 30 일부터 사용할 수있는 핫픽스가 있습니다.http://support.microsoft.com/default.aspx/kb/970944/en

안부, 팀

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top