Avatar of platinum78
platinum78

asked on 

vba code to delete duplicated items in outlook

Deleting a single message in the inbox puts two copies in the deleted items box; one read and the other unread.  I have written the attached code snippet but was unable to compile.  It attempts to locate and remove the matching copy (but not the same object) by comparing subject, sender's email address, and date/time sent.
Public Sub RemoveDuplicates()
    If ConfirmDelete = vbYes Then
        Dim ns As NameSpace
        Dim di As MAPIFolder
        Dim mi As Outlook.MailItem
        Dim diCount As Integer
        Dim miTemp As Outlook.MailItem        
        Set ns = ThisOutlookSession.Session
        Set di = ns.GetDefaultFolder(olFolderDeletedItems)
        Set Items = di.ShowItemCount
        For i = 1 To Items
            miTemp = mi
            For Each mi In di
                If Not miTemp Then
                    If miTemp.Subject = mi.Subject And miTemp.SenderEmailAddress = mi.SenderEmailAddress And miTemp.SentOn = mi.SentOn Then
                        miTemp.Delete
                        Set Items = Items - 1
                    End If
                End If
            Next
        Next
        Set ns = Nothing
        Set di = Nothing
        Set mi = Nothing
        Set miTemp = Nothing
    End If
End Sub
 
Private Function ConfirmDelete() 'asks before proceeding with delete
    Dim warningPrompt As String
    Dim warningTitle As String
    Dim warningButtons As String    
    warningPrompt = "Continue with removal of duplicated deleted items?"
    warningTitle = "Confirm Delete"
    warningButtons = vbYesNo + vbQuestion + vbDefaultButton2
    ConfirmDelete = MsgBox(warningPrompt, warningButtons, warningTitle)
End Function

Open in new window

OutlookVB Script

Avatar of undefined
Last Comment
platinum78
ASKER CERTIFIED SOLUTION
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of platinum78
platinum78

ASKER

Thank you!  Sorry, but I'm new to VBA.  Hope that wasn't too much of a headache.
Outlook
Outlook

Microsoft Outlook is a personal information manager from Microsoft, available as a part of the Microsoft Office suite. Although often used mainly as an email application, it also includes a calendar, task manager, contact manager, note-taker, journal, and web browser.

105K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo