Deleting contacts via code

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

John

Hi

I am trying to delete contacts based on a condition via code. I am using the
below code. It is vb.net so syntax is slightly different than vba.

O = New Outlook.Application

F = O.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

For I = 1 To ICount
oContact = DirectCast(F.Items.Item(I),
Outlook.ContactItem)
If InStr(oContact.Categories, "Clients") <> 0 then
oContact.Delete()
End If
Next I

The problem is that not all contacts are deleted. Presumably because when a
contact is deleted, F.Items.Item(I) does not mean anymore what it should.
Can someone tell me how can I reliably delete all contacts that fall into
the condition via code?

Thanks

Regards
 
Hi John,

in VBA you´d write:

For I = lCount To 1 Step-1

i.e. the loop has to go backwards. Another approach, often faster, is
using Index=1 as long as the collection contains items:

While Collection.Count
Remove Collection(1)
Wend
 
Back
Top