更新:2007 年 11 月
| 适用对象 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型 
 Microsoft Office 版本 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
此示例查找姓氏包含指定搜索字符串的所有联系人。
示例
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
    AccessContacts("Na")
End Sub
Private Sub AccessContacts(ByVal findLastName As String)
    Dim folderContacts As Outlook.MAPIFolder = Me.Application.ActiveExplorer() _
        .Session.GetDefaultFolder(Outlook.OlDefaultFolders _
        .olFolderContacts)
    Dim searchFolder As Outlook.Items = folderContacts.Items
    Dim counter As Integer = 0
    For Each foundContact As Outlook.ContactItem In searchFolder
        If foundContact.LastName.Contains(findLastName) Then
            foundContact.Display(False)
            counter = counter + 1
        End If
    Next
    MessageBox.Show("You have " & counter & _
        " contacts with last names that contain " _
        & findLastName & ".")
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    AccessContacts("Na");
}
private void AccessContacts(string findLastName)
{
    Outlook.MAPIFolder folderContacts = this.Application.ActiveExplorer().Session.
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
    Outlook.Items searchFolder = folderContacts.Items;
    int counter = 0;
    foreach (Outlook.ContactItem foundContact in searchFolder)
    {
        if (foundContact.LastName.Contains(findLastName))
        {
            foundContact.Display(false);
            counter = counter + 1;
        }
    }
    MessageBox.Show("You have " + counter +
        " contacts with last names that contain "
        + findLastName + ".");
}
编译代码
此示例需要:
- 在“联系人”文件夹中,姓氏包含字符串“Na”(例如,Tzipi Butnaru)的联系人。