Deleting all outlook contacts.

D

daxriders

Hi...

i've been trying to delete all my outlook contacts to update them with a DB.
The problem is that there are always some contacts left?!?

Anyone knows why and how to solve this issue?

here's my code(vb.net)

Dim olApp As New Outlook.Application
Dim olNamespace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder =
olNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

'Do While olFolder.Items.Count > 0 this option doesnt work either
For Each olItem As Outlook.ContactItem In olFolder.Items
olItem.Delete()
Next
'Loop
 
K

Ken Slovak - [MVP - Outlook]

When deleting items in a collection and using a For loop you mess with the
loop index each time you delete an item and end up deleting half the items
in each pass. Use a count down For loop for that:

For i = oFolder.Items.Count To 1 Step -1
 
S

Sue Mosher [MVP]

Never delete inside a For Each ... Next loop. Each deletion resets the
index. Instead, use a down-counting loop. Also, you should always get each
object separately, rather than using dot-operator expressions like
olFolder.Items.Count:

myItems = olFolder.Items
count = myItems.Count
For i = 1 to Count Step -1
olItem = myItems.Item(i)
olItem.Delete()
Next
 

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