I'm creating a *.pst file from email data stored in a SQL Server 2005 database. I exported some random emails from my own Outlook program into MS Access to get the table structure Outlook uses. I then deleted all records from that table and inserted the data into the table from the SQL Server 2005 database's email tables. When finished, I had 40,539 records representing emails in the table. I then used the Import Wizard in Outlook to bring that data in. When the import process finished, I had 73,305 emails imported. One of the fields I populated in my email table was the Mileage field. In that field I stored the original email message id concatenated with three pipes ("|||") concatenated with the folder id for the email (from the SQL Server 2005 database). I checked the record count both grouped and ungrouped so I know there are no duplicates in the table I imported. To weed out the duplicates Outlook created, I used the following code:

[vba]Sub Nix_Dupes_From_All_Emials()
Dim m As MailItem
Dim d As Dictionary
Dim x As Long

Set d = New Dictionary
d.RemoveAll
x = Application.GetNamespace("MAPI").Folders("Client Folders").Folders("All Emails").Items.Count

For Each m In Application.GetNamespace("MAPI").Folders("Client Folders").Folders("All Emails").Items
If Not d.Exists(m.Mileage) Then
d.Add m.Mileage, m.Mileage
Else
m.Delete
End If
Debug.Print x
x = x - 1
Next m
Set m = Nothing
d.RemoveAll
Set d = Nothing
End Sub[/vba]
I thought my logic was sound but I had to run that code four times before all the duplicates were removed and my email count in folder "All Emails" matched the record count for the table of emails I imported from MS Access.
  1. Does anyone know why I would end up with duplicates using the Wizard to import from a table that has no duplicates?
  2. Does anyone know why my code above did not nix all the duplicates the first (or second, or third) time I ran it?
Any enlightenment would be appreciated,

Mavyak