Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / Visual Basic
Article

Free way of getting rid of your duplicate emails.

Rate me:
Please Sign up or sign in to vote.
1.64/5 (7 votes)
1 May 2008CPOL3 min read 25.3K   157   4   3
Want to get rid of your duplicate emails in MS Outlook ? Already restricited trial version of plugins who do that ? This is the basic free solution.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Skillnet Solutions Pvt India Ltd.,
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat about search of same email in more than one folder Pin
john563229-Sep-09 21:06
john563229-Sep-09 21:06 
GeneralIt can be interresting ... Pin
Maximilien1-May-08 3:57
Maximilien1-May-08 3:57 
GeneralRe: It can be interresting ... Pin
John M. Drescher1-May-08 8:26
John M. Drescher1-May-08 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.