以程序化方式设置Outlook邮件的类别?

问题描述 投票:12回答:2

似乎没有太多的信息或任何好的代码示例来设置Outlook 2007 MailItem的类别。

MSDN有一个有限的页面,并提到使用VB的 分体式 功能,多少说了一些"你自己看着办吧".

据我所知,我们将Categories作为mailitem的逗号分隔的字符串属性来操作。这似乎有点原始,难道就这么简单?

大家是不是只要掏出他们的字符串函数库,解析Categories属性,相信不会在为一个mailitem设置多个类别和(天理难容)类别重命名时陷入混乱?

outlook office-interop
2个回答
23
投票

你可以选择以逗号分隔的Categories字符串进行任意操作。要插入一个类别,我通常会检查当前的字符串是否为空,然后直接赋值。如果类别不是空的,那么如果它还不存在,我就把它追加进去。要删除一个项目,我只需用一个空字符串替换类别名称。

添加类别

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

移除类别

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

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

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

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

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

0
投票

只是在@SilverNija - MSFT的帖子里面加入最后一个信息。之后做一些类似这样的事情。

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

不要忘了,这是最重要的,要这样更新mailItem的实例。

mailItem.Save();

因为,有一个问题是,有时候一堆邮件项目在循环中的时候没有更新,所以我用这个方法解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.