以编程方式设置Outlook mailitem的类别?

haw*_*bsl 12 outlook office-interop

似乎没有太多信息或任何良好的代码示例用于以编程方式设置Outlook 2007 MailItem的类别.

MSDN有一个有限的页面,并提到使用VB的Split函数,或多或少说" 你自己从这里开始,所以你自己解决 ".

据我所知,我们将类别操作为mailitem的逗号分隔字符串属性.它似乎有点原始,是不是都有它呢?

是否每个人都只是挖掘出自己的字符串函数库,并解析类别属性,相信没有一个懵懵懂懂地得到当多个类别单个的MailItem设置和(但愿)类别更名?

Sli*_*SFT 22

您可以选择以任何方式操作逗号分隔的字符串.要插入一个类别,我通常会检查当前字符串是否为空,然后只是分配它.如果类别不为null,那么如果它尚不存在则附加它.要删除项目,我只需用空字符串替换类别名称.

添加类别

 var customCat = "Custom Category";
 if (mailItem.Categories == null) // no current categories assigned
   mailItem.Categories = customCat;
 else if (!mailItem.Categories.Contains(customCat)) // insert as first assigned category
   mailItem.Categories = string.Format("{0}, {1}", customCat, mailItem.Categories);
Run Code Online (Sandbox Code Playgroud)

删除类别

var customCat = "Custom Category";
if (mailItem.Categories.Contains(customCat))
  mailItem.Categories = mailItem.Categories.Replace(string.Format("{0}, ", customCat), "").Replace(string.Format("{0}", customCat), "");
Run Code Online (Sandbox Code Playgroud)

有很多种方法可以操作字符串 - 他们只是选择保持序列化数据结构简单.

我倾向于在加载项启动期间创建自己的类别以验证它们是否存在.当然 - 类别重命名是一个问题,但如果您确保每次加载项加载时您的类别都存在,您至少可以确保某种程度的有效性.

要管理Outlook类别,可以使用ThisAddIn.Application.Session.Categories.

var customCat = "Custom Category";
if (Application.Session.Categories[customCat] == null)  
  Application.Session.Categories.Add(customCat, Outlook.OlCategoryColor.olCategoryColorDarkRed);
Run Code Online (Sandbox Code Playgroud)

  • 您需要发出[`MailItem.Save()`](http://msdn.microsoft.com/en-us/library/office/ff866979.aspx)以使用[`MailItems`](http)来保留任何元数据更改://msdn.microsoft.com/en-us/library/office/ff861332.aspx) (4认同)
  • FYI根据这个:http://msdn.microsoft.com/en-us/library/office/ff860423(v = office.15).aspx分隔符是本地化的(这是一个愚蠢的想法)`这个属性使用角色在Windows注册表中的HKEY_CURRENT_USER\Control Panel\International下的值名称sList中指定,作为多个类别的分隔符. (4认同)