Advertisement
adamchilcott

removeOutlookDuplicates.vbs

Nov 19th, 2018
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub RemoveDuplicateItems()
  2.     Dim objFolder As Folder
  3.     Dim objDictionary As Object
  4.     Dim i As Long
  5.     Dim objItem As Object
  6.     Dim strKey As String
  7.  
  8.     Set objDictionary = CreateObject("scripting.dictionary")
  9.     'Select a source folder
  10.    Set objFolder = Outlook.Application.Session.PickFolder
  11.  
  12.     If Not (objFolder Is Nothing) Then
  13.        For i = objFolder.Items.Count To 1 Step -1
  14.            Set objItem = objFolder.Items.Item(i)
  15.  
  16.            Select Case objFolder.DefaultItemType
  17.                   'Check email subject, body and sent time
  18.                  Case olMailItem
  19.                        strKey = objItem.Subject & "," & objItem.Body & "," & objItem.SentOn
  20.                   'Check appointment subject, start time, duration, location and body
  21.                  Case olAppointmentItem
  22.                        strKey = objItem.Subject & "," & objItem.Start & "," & objItem.Duration & "," & objItem.Location & "," & objItem.Body
  23.                   'Check contact full name and email address
  24.                  Case olContactItem
  25.                        strKey = objItem.FullName & "," & objItem.Email1Address & "," & objItem.Email2Address & "," & objItem.Email3Address
  26.                   'Check task subject, start date, due date and body
  27.                  Case olTaskItem
  28.                        strKey = objItem.Subject & "," & objItem.StartDate & "," & objItem.DueDate & "," & objItem.Body
  29.            End Select
  30.  
  31.            strKey = Replace(strKey, ", ", Chr(32))
  32.  
  33.            'Remove the duplicate items
  34.           If objDictionary.Exists(strKey) = True Then
  35.               objItem.Delete
  36.            Else
  37.               objDictionary.Add strKey, True
  38.            End If
  39.        Next i
  40.     End If
  41. End Sub
  42.  
  43. ' START NOTES
  44.  
  45. ' Instructions:
  46. ' I. Open Outlook
  47. ' II. Press "Alt + F11"
  48. ' III. "Insert" -> "Module"
  49. ' IV. Paste Into Module
  50. ' V. "Alt + F8" or "Run"
  51.  
  52. ' Reference:
  53. ' <https://www.datanumen.com/blogs/quickly-remove-duplicate-outlook-items-folder-via-vba/>
  54.  
  55. ' END NOTES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement