Conditionally deleting contacts

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

Could someone please show me how to, programmatically, go through a contacts
folder called clients and clear all contacts except those that have category
personal? I have written some code (given below) but don't know how to check
for the category field of a contact item and then delete the item if it does
not have personal in the category.

Thanks

Regards

O = New Outlook.Application
F =
O.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Folder
s().Item("Clients")

ICount = F.Items.Count
For I = 1 To ICount
if not {check for category field of the Ith item for 'personal'} then
{Delete Ith contact item}
end if
Next I
 
Always delete items using a down counting loop, otherwise the loop counter
gets itself confused and the indexes of the items in the collection change
for every item you delete.

Dim oContact As Outlook.ContactItem

ICount = F.Items.Count
For I = ICount To 1 Step -1
Set oContact = F.Items(I)
If Not (InStr(1, oContact.Categories, "Personal") > 0) Then
oContact.Delete
End If
Next I
 
Back
Top