Deleting contacts via code

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
 
M

Michael Bauer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top