using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
namespace OutlookAddIn20
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Application objApplication = Globals.ThisAddIn.Application;
Outlook.NameSpace objSession = objApplication.Session;
Outlook.Categories objCategories = objSession.Categories;
try
{
Outlook.MailItem objMailItem = objApplication.ActiveExplorer().Selection[1] as Outlook.MailItem;
if (objMailItem != null)
{
if (objMailItem.Categories != null)
{
string[] strCategories = objMailItem.Categories.Split(',');
foreach (string strCategory in strCategories)
{
Outlook.Category objCategory = objCategories[strCategory.Trim()]; //这一步非常重要,在有多个分类的情况下每个分类间有“, ”隔开。在使用时必须先去掉前导空格。
MessageBox.Show(objCategory.Color.ToString());
}
}
else
{
MessageBox.Show("There no flag");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message+"\n"+ex.StackTrace);
}
}
}
}
实现环境:Visual Studio 2010, Outlook 2010, .Net Framework 4.0, VSTO runtime 4.0