如何:以编程方式在联系人中搜索电子邮件地址
此示例在联系人文件夹中搜索电子邮件地址中具有域名 example.com 的联系人。
**适用于:**本主题中的信息适用于 Outlook 2013 和 Outlook 2010 的应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能。
示例
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
SearchForEmail("example.com")
End Sub
Public Sub SearchForEmail(ByVal partialAddress As String)
Dim contactMessage As String = String.Empty
Dim contacts As Outlook.MAPIFolder = Me.Application.ActiveExplorer().Session _
.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
For Each foundContact As Outlook.ContactItem In contacts.Items
If Not (foundContact.Email1Address Is Nothing) Then
If foundContact.Email1Address.Contains(partialAddress) Then
contactMessage = contactMessage & "New contact" _
& foundContact.FirstName & " " & foundContact.LastName _
& " Email Address is " & foundContact.Email1Address & _
". " & vbCrLf
End If
End If
Next
If contactMessage.Length > 0 Then
Else
contactMessage = "No Contacts were found."
End If
MsgBox(contactMessage)
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
SearchforEmail("example.com");
}
private void SearchforEmail(string partialAddress)
{
string contactMessage = string.Empty;
Outlook.MAPIFolder contacts = (Outlook.MAPIFolder)
this.Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);
foreach (Outlook.ContactItem foundContact in contacts.Items)
{
if (foundContact.Email1Address != null)
{
if (foundContact.Email1Address.Contains(partialAddress))
{
contactMessage = contactMessage + "New contact"
+ foundContact.FirstName + " " + foundContact.LastName
+ " Email Address is " + foundContact.Email1Address +
". \n";
}
}
}
if (!(contactMessage.Length > 0))
{
contactMessage = "No Contacts were found.";
}
MessageBox.Show(contactMessage);
}
编译代码
此示例需要:
- 电子邮件地址中具有域名 example.com(例如 somebody@example.com)且有名有姓的联系人。