PDA

View Full Version : Remove Contacts in Outlook



WesMo
09-30-2019, 09:25 AM
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

gmayor
10-01-2019, 05:35 AM
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

WesMo
10-01-2019, 06:56 AM
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?

WesMo
10-01-2019, 09:22 AM
I figured it out. Thanks again. Appreciate it!

gmayor
10-01-2019, 08:13 PM
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" Thenwith
If oContact.Email1Address Like "*@somewhere.com" Then