以编程方式设置Outlook mailitem的类别? [英] Setting an Outlook mailitem's category programmatically?

查看:133
本文介绍了以编程方式设置Outlook mailitem的类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There doesn't seem to much information or any good code samples for setting an Outlook 2007 MailItem's categories programmatically.

MSDN的页面受限制,并提及使用VB的 Split 函数,说或多或少的是"您从这里开始就是你自己,所以请自己解决问题".

MSDN has a limited page, and mentions using VB's Split function, saying more or less "you're on your own from here onwards, so sort it out yourself".

据我所知,我们将类别作为mailitem的逗号分隔字符串属性进行操作.似乎有点原始,是所有的东西吗?

So far as I can tell we manipulate the Categories as a comma delimited string property of the mailitem. It seems a bit primitive, is that all there is to it?

每个人都只是挖出他们的字符串函数库并解析Categories属性,是否相信当为单个mailitem设置多个类别并且重命名(禁止天堂)类别时不会陷入混乱?

Does everyone just dig out their library of string functions and parse the Categories property, trusting not to get in a muddle when multiple categories are set for a single mailitem and (heaven forbid) categories are renamed?

推荐答案

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

You can choose to manipulate the comma-delimited string of Categories any way you choose. To insert a category, I usually check if the current string is null and then just assign it. If the category is not null then I append it if it doesn't already exist. To remove an item, I just replace the category name with an empty string.

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

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

There are multitudes of ways to manipulate strings - they just chose to keep the serialized data structure simple underneath.

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

I tend to create my own Categories during Add-in startup to verify they exist. Certainly - category renaming is a concern, but if you are ensuring that your categories exist each time your add-in loads, you can at least ensure some level of validity.

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

这篇关于以编程方式设置Outlook mailitem的类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆