Delete contact from outlook contact folder in .net

 

This code sample can be used for deleting contact from outlook contacts. before using this code sample you need to add reference of ” library.

Suppose if you want to delete a contact with named ‘Ankur Gupta’ from your outlook contact folder.

[VB.Net]

    Private Sub DeleteContact()
        DeleteContactByName("Gupta", "Ankur")
    End Sub
 
    Private Sub DeleteContactByName(ByVal lastName As String, ByVal firstName As String)
        Dim oApp As Outlook.Application
        oApp = CreateObject("Outlook.Application")
        Dim oNs As Outlook.NameSpace
        oNs = oApp.GetNamespace("MAPI")
 
        Dim oFolder As Outlook.Folder
        oFolder = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
        Dim oContact As Outlook.ContactItem = TryCast(oFolder.Items. _
           Find(String.Format("[LastName]='{0}' AND [FirstName]='{1}'", lastName, firstName)),  _
            Outlook.ContactItem)
 
        If (oContact IsNot Nothing) Then
            oContact.Delete()
        End If
    End Sub

[C#]

  private void DeleteContact()
        {
            DeleteContactByName("Gupta", "Ankur");
        }
 
  private void DeleteContactByName(string lastName, string firstName)
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
            Outlook.MAPIFolder oFolder = oNS.GetDefaultFolder
                 (Outlook.OlDefaultFolders.olFolderContacts);
            Outlook.ContactItem contact = oFolder.Items.Find(string.Format 
                 ("[LastName]='{0}' AND [FirstName]='{1}'",lastName, firstName)) 
                   as Outlook.ContactItem;
 
            if (contact != null)
            {
                contact.Delete();
            }
        }