다음을 통해 공유


How to: Programmatically Delete Outlook Contacts

This example deletes a contact. The example assumes that a contact named "Armando Pinto" exists in the Contacts folder.

Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

Example

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Startup
    DeleteContact("Pinto", "Armando")
End Sub 

Private Sub DeleteContact(ByVal lastName As String, _
ByVal firstName As String)
    Dim contact As Outlook.ContactItem = _
        TryCast(Me.Application.GetNamespace("MAPI").GetDefaultFolder( _
        Outlook.OlDefaultFolders.olFolderContacts).Items. _
        Find( _
        String.Format("[LastName]='{0}' AND [FirstName]='{1}'", _
        lastName, firstName)),  _
        Outlook.ContactItem)

    If (contact IsNot Nothing) Then
        contact.Delete()
    End If 
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    DeleteContact("Pinto", "Armando");
}

private void DeleteContact(string lastName, string firstName)
{
    Outlook.ContactItem contact =
        this.Application.GetNamespace("MAPI").
    GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).
    Items.
    Find(
    string.Format("[LastName]='{0}' AND [FirstName]='{1}'",
    lastName, firstName))
    as Outlook.ContactItem;

    if (contact != null)
    {
        contact.Delete();
    }
}

See Also

Tasks

How to: Programmatically Search for a Specific Contact

How to: Programmatically Access Outlook Contacts

Concepts

Working with Contact Items