Consulting

Results 1 to 5 of 5

Thread: Remove Contacts in Outlook

  1. #1
    VBAX Newbie
    Joined
    Sep 2019
    Posts
    3
    Location

    Remove Contacts in Outlook

    I'm trying to create a script that will delete contacts that have a specific email address. The script below works, but it deletes all contacts in the contact list. Thanks!

    Code:
    Sub DeleteContacts()
    Dim myOutlook As Outlook.Application
    Dim myInformation As NameSpace
    Dim myContacts As Items
    Dim i As Long
    Dim lngCount As Long
    Set myOutlook = CreateObject("Outlook.Application")
    Set myInformation = myOutlook.GetNamespace("MAPI")
    Set myContacts = myInformation.GetDefaultFolder(olFolderContacts).Items
    lngCount = myContacts.Count
    For i = lngCount To 1 Step -1
    myContacts(i).Delete
    Next
    End Sub

  2. #2
    Maybe
    Sub DeleteContacts()Dim myOutlook As Outlook.Application
    Dim myInformation As NameSpace
    Dim myContacts As items
    Dim oContact As ContactItem
    Dim i As Long
    Dim lngCount As Long
        Set myOutlook = CreateObject("Outlook.Application")
        Set myInformation = myOutlook.GetNamespace("MAPI")
        Set myContacts = myInformation.GetDefaultFolder(olFolderContacts).items
        lngCount = myContacts.Count
        For i = lngCount To 1 Step -1
            Set oContact = myContacts(i)
            If oContact.Email1Address = "someone@somewhere.com" Then
                oContact.Delete
            End If
        Next i
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Sep 2019
    Posts
    3
    Location
    Thank you very much gmayor. It works, however I have to specify someone at somewhere.com. I'm wanting to delete any contact with an email address at somewhere.com. I removed the someone, but it doesn't do anything. Do I have to put a wildcard?

  4. #4
    VBAX Newbie
    Joined
    Sep 2019
    Posts
    3
    Location
    I figured it out. Thanks again. Appreciate it!

  5. #5
    Quote Originally Posted by WesMo View Post
    Thank you very much gmayor. It works, however I have to specify someone at somewhere.com. I'm wanting to delete any contact with an email address at somewhere.com. I removed the someone, but it doesn't do anything. Do I have to put a wildcard?
    Replace
    If oContact.Email1Address = "someone@somewhere.com" Then
    with
    If oContact.Email1Address Like "*@somewhere.com" Then
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •