Remove duplicate mail items

Status
Not open for further replies.

OverlandPark

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I am trying to write a small script by which I can remove duplicate mail items to a separate folder. I am using Outlook 2013 in a Windows 7 64-bit environment.
I would appreciate help with the following code.
I recognize this code is not efficient, but I just need to get it to run.
Thank you.


Sub RemoveDuplicates()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace

Dim myFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
Dim Duplicates As Outlook.MAPIFolder

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.PickFolder
Set Duplicates = myNameSpace.PickFolder
Set myItems = myFolder.Items

For i = 1 To myItems.Count
For j = i + 1 To myItems.Count
DoEvents
On Error Resume Next
If myItems(i).Subject = myItems(j).Subject Then
If myItems(i).ReceivedByName = myItems(j).ReceivedByName Then
If myItems(i).Sender = myItems(j).Sender Then
If myItems(i).SentOn = myItems(j).SentOn Then
If myItems(i).Body = myItems(j).Body Then

myItems(i).Move Duplicates

End If
End If
End If
End If
End If

Next
Next

End Sub
 
What happens when you run it? Any error messages?

you should count backwards... because when you delete a dup, the count changes. Although, it might not matter a whole lot since you compare ever message.

For i = myItems.Count to 1 Step -1
For j = myItems.Count to i + 1 Step -1

For efficiency, it might be better to use restrict to find the subject then compare the other values.
 
Actually, in thinking about it a little more, you don't loop J - it is just i - 1 (since you are going backwards)
For i = myItems.Count to 1 Step -1
For j = i - 1


Oh wait, that logic doesn't work good either, because it only checks the next one. I need more coffee. <g> On the other hand, it should work if there is only 1 duplicate - check message with one next to it, if match, move second message.
 
Thank you for taking the time to respond Diane.
The problem with the code is it does not seem to run all the way through.
 
it's running here - it's just a matter of getting the correct counts - you need to test each message against the other messages (easy) then move to the next message - but if you delete messages, the count gets messed up and that messes the macro up.


You may need to split it into a function - the macro gets the current message and the function loops the other messages looking for a match.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
M How to remove duplicate history items BCM (Business Contact Manager) 7
D Isn't there an easy way to remove duplicate contacts in outlook? Using Outlook 1
A Remove shared Contacts duplicate entry Using Outlook 3
P How to remove duplicate history items? BCM (Business Contact Manager) 1
B Need to merge PST, and remove duplicate items Using Outlook 3
E Edit incoming emails to remove a certain sentence added by the "system" Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
TomHuckstep Remove Send/Receive All Folders (IMAP/POP) button from Outlook 365 Ribbon Using Outlook 2
Rupert Dragwater How to permanently remove an email address Using Outlook 9
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
E Remove flag automatically Using Outlook 4
N Can't create NEW GROUP and add/remove a member from existing Group in Outlook Using Outlook 1
Timmon Remove just one attachment before AutoForward Outlook VBA and Custom Forms 0
Z Remove GMAIL IMAP account from Outlook 2016 Using Outlook 2
C-S-R Manage Add-ins (Remove Wunderlist) Using Outlook 6
O Remove duplicates within two accounts Using Outlook 2
D How to remove a folder, option grayed out Using Outlook 4
T Outlook 2016 remove envelope icon for certain folders Using Outlook 5
M In Outlook Calendar remove the buttons: 'Today' and '<' (Back a day) and '>' (Forward a day) that are below the Ribbon and above the calendar display. Using Outlook 0
P [SOLVED] Auto remove [EXTERNAL] from subject Using Outlook 16
P Add, remove, & reorder folder pane Using Outlook 6
W Remove specific contacts from contact list Outlook VBA and Custom Forms 3
T Cannot remove needless PST Using Outlook 1
Healy Consultants Macro to remove inside organization distribution list email address when reply to all recepients Outlook VBA and Custom Forms 0
S Unable to remove rule outlook 2010 Using Outlook 0
N How to remove signature formatting from Text in Word (accidentally taken from Outlook) Using Outlook 0
B Remove Subject Residual Outlook VBA and Custom Forms 3
P how to remove unwanted PST file default categories assigned to many calendar entries Using Outlook 7
J Remove text to Clean Up Outlook VBA and Custom Forms 1
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
D Remove text in subject using VBA Outlook VBA and Custom Forms 4
T Remove Old Location From Tasks Pane Using Outlook 1
A remove or turn off outlook.com contact folder from outlook 2016 Using Outlook 4
R Chancing / remove “ something ” in the subject, online archive Outlook VBA and Custom Forms 8
Morgan Fowler Remove Signature Using Outlook 1
M How to remove a list of specific contacts from Outlook Using Outlook 18
R New Links on Navigation Pane, How to Remove? Using Outlook 1
M VBA to remove deferred delivery on a MeetingItem Outlook VBA and Custom Forms 2
J Remove extra line above signature in reply Outlook VBA and Custom Forms 5
Diane Poremsky How to Remove RSS Support from Outlook Using Outlook 0
Diane Poremsky Remove Attachments From Messages Using Outlook 0
Diane Poremsky Remove Office 2013 Update Banner Using Outlook 0
Diane Poremsky Remove a password from an Outlook *.pst File Using Outlook 3
G VBA/Macro to remove page colour when replying or forwarding email Outlook VBA and Custom Forms 2
L Fake reminder apperaring (not in calendar) - how to remove? Using Outlook 5
J Your IMAP server wants to alert you to the following: cannot remove system folder Using Outlook 3
A Auto Insert of filename when selecting 'Remove Attachment' Using Outlook 1
C how to remove icons on right hand side outlook 2013 Using Outlook 2
K Remove Manage APPS button for users Exchange Server Administration 1
P Remove name and parenthses from email Using Outlook 1

Similar threads

Back
Top