Est-il possible de renommer Catégorie d'Outlook programme?
-
18-09-2019 - |
Question
Nous développons add-in Outlook 2007. Pour les tests catégorie de perspectives renommage J'ai ajouté le bloc de code suivant
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);
à la fin de complément méthode private void ThisAddIn_Startup(object sender, EventArgs e)
.
Catégorie est renommé mais si Outlook est fermé, les lignes ci-dessus sont commentées, et les perspectives est relancée - le nom de la catégorie est pas « TEST !!! » comme je l'attendais. Il est « Groupe1 » tel quel était avant de le renommer. Est-il possible de renommer la catégorie de perspectives « pour toujours » par le code? Microsoft.Office.Interop.Outlook.Category n'a pas Save () ou mise à jour () ou méthodes persist ().
P.S. Nous développons Outlook 2007 complément à l'aide de Visual Studio 2008, .NET 3.5, C # 3. Le problème est reproduit avec Outlook 2007 SP1 et SP2. D'autres versions de perspectives ne sont pas testées.
La solution
Je l'ai résolu le problème (le problème lui-même semble être Outlook 2007 bug) en utilisant un hack. Les liens suivants me ont aidé à créer le hack (oups, pas assez réputation d'afficher plus de 1 lien):
- http: //blogs.officezealot.com/legault/archive/2009/08/13/21577.aspx
- http://www.officekb.com/Uwe/Forum.aspx/outlook-prog-addins/3142/Apply-Categories-to-other-users-Outlook-2007
- http: //help.wugnet.com/office/set-master-category-list-Outlook-2007-ftopict1095935.html
- http: //forums.slipstick.com/showthread.php t = 18189
- http: //msdn.microsoft.com/en-us/library/ee203806%28EXCHG.80%29.aspx
Le hack est lui-même montrer ci-dessous:
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
}
}
}
}
}
Cette méthode d'aide doit être appelée avant la catégorie changement de nom, par exemple:.
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);
Autres conseils
Ceci est un bug Outlook introduit avec Outlook 2007 SP2.
"Considérez le scénario suivant. Vous avez une application personnalisée qui peut être exécuté pour créer de nouvelles catégories dans Outlook 2007. Vous exécutez l'application pour créer une nouvelle catégorie dans Outlook 2007. Ensuite, si vous redémarrez Outlook 2007, la catégorie que vous avez créé est supprimé de façon inattendue. Ce problème se produit après avoir installé la mise à jour Februarycumulative ou SP2. "
Il y a un correctif disponible depuis le 30 Juin, 2009: http://support.microsoft.com/default.aspx/kb/970944/ en
Cordialement, Tim