Delete Duplicate Emails in Outlook Using VBA Code

Picture this: you open your Outlook, and it’s flooded with duplicate emails. Annoying, right?

Manually deleting them feels like a never-ending game of whack-a-mole.

But what if there was a magic wand that could make these duplicates disappear with just a few clicks?

Well, that’s where VBA (Visual Basic for Applications) comes into play.

In this post, I’m going to show you how to use VBA code to effortlessly delete duplicate emails in Outlook, saving you time and sanity.

The VBA Code

Sub DeleteDuplicateEmails()
    Dim OutlookApp As Outlook.Application
    Dim Namespace As Outlook.NameSpace
    Dim Folder As Outlook.MAPIFolder
    Dim MailItem As Object
    Dim i As Integer
    Dim Dict As Object
    Set Dict = CreateObject("Scripting.Dictionary")
    Set OutlookApp = New Outlook.Application
    Set Namespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = Namespace.GetDefaultFolder(olFolderInbox)

    For Each MailItem In Folder.Items
        If Dict.Exists(MailItem.Subject & MailItem.ReceivedTime) Then
            MailItem.Delete
            i = i + 1
        Else
            Dict.Add MailItem.Subject & MailItem.ReceivedTime, MailItem
        End If
    Next MailItem

    MsgBox i & " duplicate emails were deleted."
End Sub

Understanding the VBA Code

  1. Setting Up the Stage: The script begins by creating objects – OutlookApp, Namespace, Folder, and MailItem – to interact with Outlook.
  2. Tracking Uniques: It uses a Scripting.Dictionary (Dict) to keep track of unique emails based on their subject and received time.
  3. The Cleaning Process: As it loops through the emails, it checks if an email’s unique identifier already exists in Dict. If it does, the email is a duplicate and gets deleted.
  4. Counting the Culprits: It keeps a tally of how many duplicates are removed, which is displayed at the end.

Adding VBA to Outlook

  1. Open VBA Editor in Outlook: Press Alt + F11 [Keyboard Shortcut] to open Outlook, or click on the development tab from the ribbon.
  1. Click on Visual Basic to open the VBA editor.
  1. Create a New Module: Right-click on ‘ThisOutlookSession’, select ‘Insert’, then ‘Module’.
  1. Copy-Paste the Magic: Place the VBA code into this module.
  2. Save Your Work: Just hit Ctrl + S.

Step-by-Step: Using the VBA Code

  1. Macro Settings: Before running the script, make sure macros are enabled in your Outlook settings.
  2. Running the Show: With the VBA editor open (Alt + F11), select the script and press F5 to run it.
  3. Confirmation: A message will pop up with the number of deleted duplicates.

Conclusion

Embracing VBA to delete duplicate emails in Outlook is like having a personal assistant for your inbox.

It’s a powerful tool that can do the heavy lifting for you, turning a potentially hours-long task into a quick, painless process.

While it might seem daunting at first, especially if you’re new to programming, the benefits are undeniable.

From saving time to ensuring precision in clearing duplicates, VBA can be a game-changer.

Related Articles