• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Delete duplicate messages using a macro

Slipstick Systems

› Developer › Code Samples › Delete duplicate messages using a macro

Last reviewed on May 2, 2024     34 Comments

A client had many duplicates in one folder and while there are a number of duplicate remover utilities, any of which work great if you need to check multiple folders for duplicates, he just needed to check one folder. He didn't want to install a utility but there were too many duplicates to manually remove them.

The solution: a macro that moves duplicate messages to a subfolder for review.

This macro is slow. If you have a lot of messages in the folder, expect it to take some time to run. See Duplicate Remover Tools for third party utilties.

To use, select a folder that needs checked for duplicates and run the macro. The macro will create a subfolder named Duplicates and move the duplicate messages to it. After reviewing the duplicates, delete the folder.

move duplicate messages

Public Sub MoveDuplicates()
    Dim objOL As Outlook.Application
    Dim objFolder As Outlook.MAPIFolder
    Dim objDupFolder As Outlook.MAPIFolder
    Dim objDictionary As Object
    Dim i As Long
    Dim objItem As Object
    Dim strKey As String
    
    Set objOL = Outlook.Application
    Set objDictionary = CreateObject("scripting.dictionary")
    Set objFolder = objOL.ActiveExplorer.currentFolder
    
    On Error Resume Next
    Set objDupFolder = objFolder.Folders.Item("Duplicates")
    If objDupFolder Is Nothing Then
        Set objDupFolder = objFolder.Folders.Add("Duplicates")
    End If

       For i = objFolder.Items.count To 1 Step -1
           Set objItem = objFolder.Items.Item(i)
        'Only check email items type
           If InStr(1, objItem.MessageClass) <> "IPM.Schedule" Then
          
            strKey = objItem.Subject & "," & objItem.Body & "," & objItem.SentOn
            strKey = Replace(strKey, ", ", Chr(32))
            If objDictionary.Exists(strKey) = True Then
              objItem.Move objDupFolder
            ' use this to delete immediately
            ' objItem.Delete
           Else
              objDictionary.Add strKey, True
           End If
   End If
   
       Next i

End Sub

How to use the macros on this page

First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.

To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

The macros on this page should be placed in a module.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

More information as well as screenshots are at How to use the VBA Editor

More Information

See Duplicate Remover Tools for third party utilities that will be somewhat faster and have more features.

Delete duplicate messages using a macro was last modified: May 2nd, 2024 by Diane Poremsky
Post Views: 70

Related Posts:

  • Duplicate Appointment Remover Tools
  • Delete Duplicate Outlook Contacts
  • Combine Duplicate Contact Items
  • View Messages in Internet Explorer using a macro

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Comments

  1. SC SC SC says

    May 22, 2024 at 9:56 pm

    Thanks! it works great for my own inbox, but I have several shared mailboxes attached to my outlook. How can I made the Macro run on these 'other' added inboxes too?

    Thanks in advance!
    SC

    Reply
    • Diane Poremsky says

      May 31, 2024 at 4:55 pm

      This line: Set objFolder = objOL.ActiveExplorer.currentFolder Means it runs on the selected folder.

      It creates the duplicate folder as a subnfolder of the selected folder, so it should work on any folder in any mailbox.

      Reply
      • SC SC SC says

        June 3, 2024 at 11:02 pm

        Oh right, my bad. Is there a way it can be run in at the "mailbox level" which automatically applies to all subfolders within the mailbox, or does it only work when run within each folder?

      • Diane Poremsky says

        June 4, 2024 at 7:29 am

        It's per-folder - mostly because it is slow and inefficient. If you need to do multiple folders, use a commerical product such as Relief jet.
        https://www.slipstick.com/addins/duplicates-addins/duplicate-remover-tools-for-outlook/

  2. Helmer Aslaksen says

    August 13, 2022 at 6:49 pm

    Thanks!!! Just what I needed! You saved me a lot of work! You're my hero! And thanks for also explaining what the Outlook clean up function does.

    Reply
  3. Bikram singhary says

    June 28, 2022 at 12:34 am

    There are some tips for Outlook Delete Duplicate Emails ;

    • Run MS Outlook >> select Clean Up on Outlook ribbon window. Expand it.
    • There are three options “Clean Up Conversation”, “Clean Up Folders”, “Clean Up Folders & Subfolders” >> choose “Clean Up Folders & Subfolders”.
    • Click on “Yes” & proceed.
    • You’ll see a dialogue box “Clean Up Folder”. Hit on “Clean Up Folder”.
    • Another option is “Settings”, select the “Settings” option to manually choose the destination of deleted items folder.
    • Now click “Browse” to provide the data file location.
    • On a pop-up Select Folder (displaying all the available folders in MS Outlook). Choose Deleted Items & then press OK.
    Reply
    • Diane Poremsky says

      June 28, 2022 at 10:42 pm

      Clean up was not designed to remove duplicates - it cleans up redundant messages in threads. For example, if you have a long thread back and forth with each person quoting the messages in full, all messages in the thread except for the last one will be cleaned up.

      Reply
  4. David Pierson says

    June 27, 2022 at 8:03 am

    Hi, Thank you this is just what I was looking for.
    Puzzled by the line

    If InStr(1, objItem.MessageClass) <> "IPM.Schedule"
    

    because Instr normally takes two string arguments after the start position.
    InStr([ start ], string1, string2, [ compare ])
    Thanks again.

    Reply
    • Diane Poremsky says

      June 27, 2022 at 9:26 am

      This should also work -
      If looking for true -
      if InStr(1, objItem.MessageClass, "IPM.Schedule")

      if looking for not true -
      If InStr(1, objItem.MessageClass, "IPM.Schedule") = 0 Then

      Reply
  5. Amit says

    September 5, 2021 at 6:18 am

    below message appears whenever i try to run it:

    user-defined type not defined

    Reply
    • Diane Poremsky says

      September 5, 2021 at 7:26 am

      if you click debug, which line does it stop on? That error usually means a reference to another object model is not set, but this macro doesn't use other references.

      Reply
  6. Gonzalo says

    June 20, 2021 at 1:57 pm

    Hello, your code works amazingly.
    Do you think it's possible that instead of moving duplicates to a new folder to have them marked/labeled with a color tab to be able to visually check the detected duplicates before deleting them?
    You are a life saver

    Reply
    • Diane Poremsky says

      June 20, 2021 at 2:45 pm

      Sure. Change this line:

       objItem.Move objDupFolder

      to

       objItem.categories = "duplicate"

      Reply
  7. Nasser says

    April 28, 2021 at 7:49 am

    hello, is it possible to edit it to delete the duplicated message from the inbox instead of the subfolder (keep the message in the subfolder safe)

    Reply
    • Diane Poremsky says

      April 28, 2021 at 8:58 am

      It's moving the dupe to the subfolder - you can delete it instead.

      If objDictionary.Exists(strKey) = True Then
             objItem.Move objDupFolder
            ' use this to delete immediately
            ' objItem.Delete
            Else

      Reply
  8. Dixit says

    April 26, 2021 at 9:18 am

    Copied/Pasted, worked like a charm. Thx a lot

    Reply
  9. Yogesh D. Khebde says

    February 22, 2021 at 9:14 am

    Hi Diane,

    Im Yogesh and i work as an Business analyst - hence i keep recieving a lot of automated reports from our different sources - and i have set rusel for all of them to be seggregated and moved to different folders - however some times there are emails / reports with same subject line and sender that get moved to multiple different folders (obviously because of the rules i set in incorrect order - which i must admit im lazy to fix) . hence i was checking if i can get a macro to check all folders and its subfolders for the emails with same Subject - Time - Sender & Body and delete any of them - the macro is BBOOS here to decide.

    Im Ok with any 1 copy staying there for my reference.

    is it possible ? or am i asking for too much from a macro ..!

    thanks a ton for the current one too ...!

    Reply
    • Diane Poremsky says

      April 28, 2021 at 11:09 pm

      it would be very slow to check all folders - an addin would be more efficient - still slow, but faster because its complied.

      Reply
  10. Neris says

    February 6, 2021 at 3:45 pm

    Hi, The outlook 2016 32bit crashes when I try to run this code! Any idea? Thanks

    Reply
    • Diane Poremsky says

      February 8, 2021 at 12:51 am

      No error messages, it just crashes? Does it process some message before crashing? How many messages are in the folder?

      Reply
  11. Ben Watson says

    November 19, 2020 at 9:44 pm

    Thank you, I've used your tutorials over the years and they are always very helpful!

    Reply
  12. Jose Grande says

    September 21, 2020 at 2:18 pm

    strKey 
    

    comes up as an empty string, and as such, it fails to find real duplicates.

    Reply
  13. Timothy Dollimore says

    May 5, 2020 at 8:58 pm

    Hi

    This is great. Its just chopped a folder of messages in half for me, so thanks.

    But :-) I think this should also check for attachments being the same. If you have two copies of a message and one of them has had the attachments removed, the current code will move one of the copies to the duplicates folder (potentially the one with the attachment). And if you don't notice :-(

    I tried adding the 'Attachments' attribute to the comparison, but that didn't distinguish between a message with attachment, and a copy of the message with the attachment deleted.

    So, instead I opted for LastModificationTime

    strKey = objItem.Subject & "," & objItem.Body & "," & objItem.SentOn & "," & objItem.LastModificationTime

    Some minor testing undertaken, the worst case scenario if you add this extra thing to the key that is built is that some messages won't be deleted (that you might consider duplicates)

    Reply
  14. fffffffrrf says

    February 11, 2020 at 9:54 pm

    Thx bande de bitches de vos meres les sac a foutre qui se font trouer le cul sur le trottoir

    Reply
  15. Hazem briki says

    July 7, 2019 at 9:24 pm

    hi , i really like your work and that help me a lot i want to customize your code to depend of my needs but i stucked ,
    1 st how can i excute this macro in a specefic folder not in current folder

    2nd how can i remove the duplicated mails but it depend on time
    for exemple :

    if
    input : 3 mails same body (the time between 3 mails is less than 30 minutes)
    output: 1 mail in folder and 2 in the duplicates folder

    if
    input : 3 mails same body ( the time between 3 mails is more than 30 minutes)
    output : 3 mails in the folder and 0 in the duplactes folder

    i hope you can anwser my question and thank you a lot

    Reply
  16. Johannes Czernin says

    April 13, 2019 at 6:11 am

    My Outlook Journal folder does contain a fair number of duplicates. Would it bei possible to modify this script to get rid of these duplicates?

    Reply
    • Diane Poremsky says

      April 13, 2019 at 11:39 pm

      It should work - you need to change this line
      If InStr(1, objItem.MessageClass) <> "IPM.Schedule" Then
      to
      If InStr(1, objItem.MessageClass) <> "IPM.Activity" Then

      You need to change the Senton field to start
      objItem.SentOn

      Reply
      • Johannes Czernin says

        April 14, 2019 at 12:42 pm

        Hi, Diane, you're great! Throughout the years, with a number of program updates, hard disk replacements and computer breakdowns, I had assembled no less than 23.400 Journal entries.

        First I tried your code, in a separate .pst file, on 32 entries and within seconds 27 of them were sorted out as duplicates. Then I went the whole way and gave it the go on the full directory. After slightly more than two hours exactly 9.065 of them had been removed as duplicates.

        Thanks a lot for your great help, and best wishes from Vienna, Austria,

        Johannes

      • Diane Poremsky says

        April 16, 2019 at 12:30 am

        You're welcome!

        Thanks for mentioning how longi t takes- i knew it was slow if there were a lof of items ot checl - but never timed it.

      • Robin Kaspersen says

        April 23, 2019 at 7:11 am

        Hi Diane, I am trying to use this VBA. Which works great. However, I wan tit to remove the oldest of the duplicates and keep the newest addition. As it is now it is deleting the newest and keeping the old. How can I change this to delete the old and keep the newest added?

      • Diane Poremsky says

        April 23, 2019 at 4:07 pm

        It's seeing the oldest first, so it gets added to the dictionary first. You could try changing the sort order, but I don't think it will help. Changing the count won't help because you need it stepping backwards.

        I was going to suggest changing the modified date of each after
        If objDictionary.Exists(strKey) = True Then

        but i don't think that would work since its using a 'dictionary'. You'd need to add the modified field to this line
        strKey = objItem.Subject & "," & objItem.Body & "," & objItem.SentOn
        then check it, but offhand, I don't know how since the date isn't being saved as a date.

        So... if changing the sort order doesn't help, i dont know of a way to do it. Sorry. :(

      • Robin Kaspersen says

        April 24, 2019 at 8:00 am

        Thanks for the feedback. It does not seem sort helps when using (i).

        I tried to see if the sorting could work by using the following VBA below.
        It works with this, but it does not check all the duplicates and remove, only a selected handfull (like 3-4 cases). I need it do check the entire calendar and delete the oldest in 1 run.

        Sub RemoveDuplicateItems()
        Dim objDictionary As Object
        Dim i As Long
        Dim objItem As Object
        Dim strKey As String
        Dim objFolder As Folder
        Dim objItems As Outlook.Items

        Set objDictionary = CreateObject("scripting.dictionary")
        'Select a source folder
        Set objFolder = Outlook.Application.Session.PickFolder
        Set objItems = objFolder.Items
        objItems.Sort "[CreationTime]", True

        For Each objItem In objItems

        Select Case objItem.Class
        'Check appointment subject
        Case olAppointment
        strKey = objItem.Subject
        End Select
        strKey = Replace(strKey, ", ", Chr(32))
        'Remove the duplicate items
        If objDictionary.Exists(strKey) = True Then
        objItem.Delete
        Else
        objDictionary.Add strKey, True
        End If
        Next objItem
        End Sub

      • Johannes Czernin says

        December 18, 2019 at 3:29 pm

        Hi, Diane,

        Due to some malware I had to completely re-install my system with Win10 and Office 365, the latter in the 64-bit version instead of the 32-bit one I had been sing before. When I had everything working properly I tried to run your MoveDuplicates macro but as soon as I had pressed the F5 button a message box popped up with the information that "... macros in this object are disactivated…"

        Disabling all Trust Center restrictions has not helped at all. Proceeding to every bit of information and hints I could find by a Google search was in vain. Most of these tips on the Net just repeat the original Outlook "Help" function whilst those that do not are crap as they are citing text formulated for older versions of Office.

        My faith in your Guru capability is endless so please could you once more help me out with some advice?

        Thanks a lot for any help and kind regards,

        Johannes

      • Diane Poremsky says

        December 18, 2019 at 4:18 pm

        It should work to set macro security to low (at least temporarily, then use selfcert and sign it and go back to signed macros) - but you do need to restart outlook for it to go into effect.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 5

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Sync Issues and Errors with Gmail and Yahoo accounts
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Sync Issues and Errors with Gmail and Yahoo accounts

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.