Outlook 2007 插件使用 Microsoft Visual C#.NET - 第二部分






4.77/5 (11投票s)
我们如何通过“Microsoft.Office.Interop.Outlook”委托和事件与 Microsoft Office Outlook 进行通信。
目录
- 引言
- 我们学习什么?
- 如何创建 Outlook7 插件项目
- 如何创建类别标签
- 如何将菜单项添加到 Outlook 应用程序的上下文菜单中
- 参考
- 结论
引言
去年,我写了一篇关于“Outlook 2007 插件使用 Microsoft Visual C#.NET”的文章,其中我们讨论了如何为 Microsoft Office Outlook 2007 开发插件,以帮助一个虚构的公司。如何添加自定义菜单、工具栏命令按钮、自定义选项卡(Outlook 2007)、处理 Ribbon 和自定义窗体等。总之,如果您是初学者,我建议您从下面的链接阅读我的第一篇文章。
好吧,如果您熟悉了,那么就可以继续了。
*** *请注意,我使用了 Microsoft Visual Studio 2010,Framework 4.0。*
我们学习什么
在本文中,我们不讨论创建自定义菜单、工具栏命令按钮、自定义选项卡等。我们的主要目标是学习如何通过“Microsoft.Office.Interop.Outlook
”委托和事件与 Microsoft Office Outlook 进行通信。让我们设想一下,我们要创建一个 Outlook 插件,该插件能够做到以下几点:
- 目标 - 1. 新邮件到达时通知您
- 目标 - 2. 当您单击特定对象(如电子邮件或子文件夹等)时,显示项目位置(路径)。
- 目标 - 3. 能够创建新的类别标签。
我认为这相当有趣!不是吗? 那么,我们还在等什么?让我们开始创建这个插件吧。有关 Outlook API 委托和事件的更多信息,请访问此链接。
创建 Outlook7 插件项目
我希望您已经掌握了如何使用 Microsoft Visual Studio 2008 或其 2010 年 5 月版创建 Microsoft Office Outlook 插件项目的基本知识。如果您不熟悉,我建议您通过此链接阅读本文的第一部分。
好的,让我们来实现我们的目标。 从您的 Microsoft Visual Studio 2008 / 2010 中创建一个 Microsoft Office Outlook 插件项目。现在我们将编写第一个代码来实现我们的第一个任务,即在新邮件到达时通知您。但我们该如何做到呢?
目标 1. 新邮件到达时通知您
嗯,我们将为此使用一个委托(ApplicationEvents_11_NewMailEventHandler Delegate
)。您可能会问这个委托是做什么的? 简单地说,这是一个对应对象中事件的委托。以下代码片段示例将实现我们的第一个目标。
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += new ApplicationEvents_11_NewMailEventHandler(EmailArrived);
}
private void EmailArrived()
{
MessageBox.Show("Hello !!! You have new emails. ");
}
下图 A 显示了上述代码的输出。
图A
有关ApplicationEvents_11_NewMailEventHandler Delegate
的更多信息,请访问此链接。
目标 - 2. 单击特定对象(如电子邮件或子文件夹等)时,显示项目位置(路径)。
为了获取项目的具体位置/路径,我们将为此使用另一个委托(ApplicationEvents_11_ItemLoadEventHandler
)。下面的代码片段完成了这项工作。
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemLoad +=
new ApplicationEvents_11_ItemLoadEventHandler(GetMailItemLocation);
}
private void GetMailItemLocation(object Item)
{
if (outlookObj.ActiveExplorer().CurrentFolder != null)
{
var mailItem = outlookObj.ActiveExplorer().CurrentFolder;
Outlook.MAPIFolder oFolder = (Outlook.MAPIFolder)mailItem;
MessageBox.Show("Hello !!! I am located at
{" + oFolder.FolderPath.ToString() + "}");
}
}
下图 B 显示了上述代码的输出。
图 B
有关 ApplicationEvents_11_ItemLoadEventHandler
的更多信息,请访问此链接。
如何创建类别标签
目标 3. 能够创建新的类别标签
好的,太棒了!现在我们将创建一个自定义类别。嗯,Microsoft Office Outlook 有一些类别,所以我们需要进行一点检查,看看我们正在创建的类别是否已存在。总之,下面的代码将创建一个类别。
private void AddCategory(string categoryName, object olCategoryColor)
{
if (AllowToAddCategory(categoryName))
{
outlookObj = new Outlook.Application();
Outlook.Categories olCategoryList = outlookObj.Session.Categories;
olCategoryList.Add(categoryName,
Outlook.OlCategoryColor.olCategoryColorDarkOrange);
MessageBox.Show("Category [ " + categoryName + " ]
successfully created.");
this.Close();
}
else
{
MessageBox.Show("This category already exist.");
}
}
private bool AllowToAddCategory(string categoryName)
{
bool retValue = false;
outlookObj = new Outlook.Application();
Outlook.Categories olCategoryList = outlookObj.Session.Categories;
foreach (Outlook.Category category in olCategoryList)
{
if (category.Name.Equals(categoryName))
{
retValue = false;
return retValue;
}
else { retValue = true; }
}
return retValue;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttonOK_Click(object sender, EventArgs e)
{
if (this.textBoxCategory.Text != null)
{
this.AddCategory(textBoxCategory.Text, null);
}
}
下图 C 显示了输入用于创建新类别的类别名称。
图 C
下图 D 显示了我们新类别的输出。
图 D
如何将菜单项添加到 Outlook 应用程序上下文菜单中
要将项目添加到 Microsoft Office Outlook 的上下文菜单中,我们需要使用 ApplicationEvents_11_ItemContextMenuDisplayEventHandler
委托。有关 ApplicationEvents_11_ItemContextMenuDisplayEventHandler
的更多信息,请访问此链接。
下面的代码片段将自定义菜单项添加到应用程序的上下文菜单中。
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
olInspectors = this.Application.Inspectors;
olInspectors.NewInspector +=
new InspectorsEvents_NewInspectorEventHandler(GetMailItemEntryId_Click);
outlookObj = new Outlook.Application();
this.Application.ItemContextMenuDisplay +=
new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler
(Application_ItemContextMenuDisplay);
this.Application.NewMail +=
new ApplicationEvents_11_NewMailEventHandler(EmailArrived);
this.Application.ItemLoad +=
new ApplicationEvents_11_ItemLoadEventHandler(GetMailItemLocation);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
#region Private Methods
private void Application_ItemContextMenuDisplay
(Office.CommandBar CommandBar, Outlook.Selection Selection)
{
if (Selection[1] is Outlook.MailItem)
{
this.CustomContextMenu(CommandBar, Selection);
this.CustomContextCategoryMenu(CommandBar, Selection);
}
}
private void CustomContextCategoryMenu
(Office.CommandBar CommandBar, Outlook.Selection Selection)
{
Office.CommandBarButton customContextMenuTag =
(Office.CommandBarButton)CommandBar.Controls.Add
(Office.MsoControlType.msoControlButton
, Type.Missing
, Type.Missing
, Type.Missing
, true);
customContextMenuTag.Caption = "Create category";
customContextMenuTag.FaceId = 445;
customContextMenuTag.Click +=
new Office._CommandBarButtonEvents_ClickEventHandler
(customContextMenuCategoryTag_Click);
}
private void CustomContextMenu(Office.CommandBar CommandBar,
Outlook.Selection Selection)
{
Office.CommandBarButton customContextMenuTag =
(Office.CommandBarButton)CommandBar.Controls.Add
(Office.MsoControlType.msoControlButton
, Type.Missing
, Type.Missing
, Type.Missing
, true);
customContextMenuTag.Caption = "My Menu";
customContextMenuTag.FaceId = 446;
customContextMenuTag.Click +=
new Office._CommandBarButtonEvents_ClickEventHandler(customContextMenuTag_Click);
}
#endregion
#region Events Handler
private void customContextMenuTag_Click
(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show("Hello !!! Happy Programming. ");
}
private void customContextMenuCategoryTag_Click
(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
CategoryDialog categoryDialog = new CategoryDialog();
categoryDialog.ShowDialog();
}
#endregion
}
下图 E 显示了创建的菜单项。
图 E
参考
- Microsoft 开发网络
结论
我希望这可能对您有所帮助!!!
历史
- 2011 年 8 月 27 日:初始帖子