Deleting all outlook contacts.

  • Thread starter Thread starter daxriders
  • Start date Start date
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
 
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
 
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
 
Back
Top