65.9K
CodeProject 正在变化。 阅读更多。
Home

Outlook、VSTO 和 C# 互操作简介

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.57/5 (10投票s)

2007 年 5 月 29 日

CPOL

2分钟阅读

viewsIcon

65787

downloadIcon

1052

本文向您展示了如何使用 VSTO 创建 Outlook 加载项并读取联系人信息

Screenshot - outlook1.png

引言

本文档展示了如何创建一个简单的 Microsoft Office Outlook Addin,它添加一个按钮并显示指定文件夹中联系人的一些信息。

这是我在 CodeProject 上的第一篇文章。希望它能提供你所需的一切!

背景

此示例使用 VSTO 2005 SE (Visual Studio Tools for Office 2005 SE) 创建 Addin。

你可以在这里获取 VSTO。

必须安装 Microsoft Office(至少 Outlook)!

Using the Code

我假设你能够使用 VSTO 创建 Outlook addin。如果你不能,你应该阅读相关文章(MSDN 提供了大量信息)或使用本文提供的示例项目。

请查看示例源代码以获取实现细节!

首先,我们需要为我们的 addin 创建一个按钮

//First we try to get the default toolbar
Office.CommandBars cmdBars = Application.ActiveExplorer().CommandBars;

//In the German version of Outlook the default command bar
//is named "Standard". In other language versions of Outlook
//this name could differ, then you need to modify the next line
Office.CommandBar cmdBar = cmdBars["Standard"];

//Try to get the existing Button
try
{
    m_btnSampleAddin = (Office.CommandBarButton)cmdBar.Controls[SampleButtonTag];
}
catch (Exception)
{
    m_btnSampleAddin = null;
}

//If the button doesn't exist, create a new one
if (m_btnSampleAddin == null)
{
    m_btnSampleAddin = (Office.CommandBarButton)cmdBar.Controls.Add(
    1, missing, missing, missing, missing);

    m_btnSampleAddin.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
    m_btnSampleAddin.Caption = SampleButtonTag;
    m_btnSampleAddin.Tag = SampleButtonTag;
}

//Add a click handler for this button
m_btnSampleAddin.Click +=
 new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
		(m_btnSampleAddin_Click);

在新建按钮的点击事件处理程序中,我们让用户使用默认文件夹选择对话框选择一个 Outlook 文件夹

Screenshot - outlook4.png

Outlook.MAPIFolder folder = Application.Session.PickFolder(); 

如果用户选择了文件夹并点击“确定”确认此操作,则“folder”的值将不为 null。在这种情况下,我们将显示“contacts”对话框并将选定的文件夹传递给构造函数。

if (folder != null)
{
    ContactDisplay contactDisplay = new ContactDisplay(folder);
    contactDisplay.Show();
} 

在 contacts 表单的加载方法中,我们将遍历指定文件夹中的所有对象。如果对象是“contact”项目,我们将将其添加到我们的列表控件中

foreach (object objItem in m_folder.Items)
{
    //if the item is an Outlook contact, display its information in the listview
    if (objItem is Outlook.ContactItem)
    {
        Outlook.ContactItem contact = objItem as Outlook.ContactItem;
        //Fill the list view item with some information about the contact
        ListViewItem lvi = new ListViewItem(new string[]{
            contact.FirstName,
            contact.LastName,
            contact.FullName,
            contact.Email1Address
        });

        //and finally set the contact as the list view item's tag so we
        //later can access this contact object
        lvi.Tag = contact;

        lvContacts.Items.Add(lvi);
    }
} 

重要的是在 ListViewItem 的 Tag 中保留 ContactItem 对象,以便我们稍后可以访问该项目!

Screenshot - outlook5.png

如果你双击 listview 项目,我们将读取 tag 并将其转换为 Outlook ContactItem。此后,我们将显示该项目的检查器。

//If a list view item has been selected
if (lvContacts.SelectedItems.Count > 0)
{
    ListViewItem lvi = lvContacts.SelectedItems[0];

    //get the contact information from the item tag
    Outlook.ContactItem contact = lvi.Tag as Outlook.ContactItem;

    //and display the outlook inspector of the contact
    contact.GetInspector.Display(Type.Missing);
} 

Inspector 是一个几乎适用于每个 Outlook 项目的对象,它会向你显示该项目的默认 Outlook 视图。

关注点

使用 Office 的旧组件模型,你必须处理一些限制(例如,你无法在未经用户事先授权的情况下读取联系人的电子邮件地址)。使用 VSTO 创建 Office Addins 也更容易!

历史

  • 2007 年 5 月 31 日:文章已更新
  • 2007 年 5 月 30 日:初始发布
© . All rights reserved.