Introduction
This code would help you get rid of the duplicate emails you have in your MS Outlook client.
Background
I have imported my gmail emails into my MS Outlook client. But since
the connection was not so good I ended up downloading the same email
multiple times. So in the end I had downloaded 6000 emails. Out of
which I guess at least 1000 were duplicates. I desperately wanted a software that could help me get rid of all those duplicate emails. I googled the internet to only find some trial version softwares that would only find a small number of emails and then ask you to register to clean-up your outlook folder. So I decided to write some VB macro code myself to do the task. Actually I am java programmer but I believe what once my friend Hadi told me "once a programmer is always a programmer". So with that belief I coded this small utility code in massive 4 hours. But anyways I achieved what I wante
Algorithm
Get all Emails from the current selected folder into EmailList
Sort EmailList based on ReceivedDate and store into SortedEmailList
Sorted Email List - SortedEmailList
A blank integer array MailsToBeDeletedArray, Length - 1000 (Length of the integer array defines how many duplicates would be deleted in my execution).
Integer NoOfEmailsToBeDeleted = 0
Begin Loop on SortedEmailList using Counter X
Get Mail Item X from the Sorted Email List
Generate the Email signature for item X (Signature is a string containing Email's Subject, Received Date, SentOn and SenderEmail concatenated)
Get Mail Item X+1 from the Sorted Email List
Generate the Email signature for item X+1
If Signature of X mail item is same as Signature of X+1 mail item
Set MailsToBeDeletedArray[NoOfEmailsToBeDeleted] = X+1
End Loop
Begin Loop on MailsToBeDeletedArray using Counter X
Get Integer mailIndex at X in MailsToBeDeletedArray
if mailIndex = 0
End Loop
Get all Emails from the current selected folder into EmailList ' This line is present because of a bug#1 I couldnt solve
Sort EmailList based on ReceivedDate and store into SortedEmailList ' This line is present because of a bug#1 I couldnt solve
Get Email at (mailIndex-X) in SortedEmailList
Email.Delete
End Loop
Problems
Any help / idea about this bug would be helpful.
Bug#1 -
I was getting error Run-time error '-2147221233, method 'delete' of object 'MailItem' failed
I was able to analyze that after calling the delete method, the email are not getting deleted but has been marked as deleted.
So next time when you try to delete the email at the same index, it throws this error.
So I solved it by re-fetching the emails from the folder and sorted them again.
I know it would be a performance overhead but all my attempts to solve this bug had failed.
Using the code
To use this code,
Open your MS Outlook client.
Press Alt + F11, that would open up the Microsoft Visual Basic Editor.
Click Insert > Module (default name should be OK)
Open the newly created module in the editor.
With the paste the code from the attached zip file over there.
With the folder selected (in which you want to delete your duplicate emails), go back to VB Editor press F5.
Points of Interest
Since I dont have much experience in VB, any suggestions / modifications / improvement would be appreciated.
History
no HISTORY as yet.