如何:以编程方式访问 Outlook 联系人
此示例查找姓氏包含指定搜索字符串的所有联系人。
**适用于:**本主题中的信息适用于 Outlook 2013 和 Outlook 2010 的应用程序级项目。有关更多信息,请参见按 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
MsgBox("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 + ".");
}
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)的联系人。