HOW TO:以程式設計方式搜尋特定的連絡人
這個範例會在 Outlook 的 [連絡人] 資料夾中,按照名字和姓氏搜尋特定的連絡人。此外,此範例會假設 [連絡人] 資料夾中有名為 John Evans 的連絡人。
**適用於:**本主題中的資訊適用於 Outlook 2013 和 Outlook 2010 的應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
範例
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
FindContactEmailByName("John", "Evans")
End Sub
Private Sub FindContactEmailByName(ByVal firstName As String, _
ByVal lastName As String)
Dim outlookNameSpace As Outlook.NameSpace = Me.Application.GetNamespace("MAPI")
Dim contactFolder As Outlook.MAPIFolder = _
outlookNameSpace.GetDefaultFolder( _
Outlook.OlDefaultFolders.olFolderContacts)
Dim contactItems As Outlook.Items = contactFolder.Items
Try
Dim contact As Outlook.ContactItem = _
CType(contactItems.Find(String.Format _
("[FirstName]='{0}' and [LastName]={1}", _
firstName, lastName)), Outlook.ContactItem)
If contact IsNot Nothing Then
contact.Display()
Else
MsgBox("The contact information was not found.")
End If
Catch ex As Exception
Throw ex
End Try
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
FindContactEmailByName("John", "Evans");
}
private void FindContactEmailByName(string firstName, string lastName)
{
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder contactsFolder =
outlookNameSpace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.
OlDefaultFolders.olFolderContacts);
Outlook.Items contactItems = contactsFolder.Items;
try
{
Outlook.ContactItem contact =
(Outlook.ContactItem)contactItems.
Find(String.Format("[FirstName]='{0}' and "
+ "[LastName]='{1}'", firstName, lastName));
if (contact != null)
{
contact.Display(true);
}
else
{
MessageBox.Show("The contact information was not found.");
}
}
catch (Exception ex)
{
throw ex;
}
}