我試圖創建一個VBA宏來檢查是否有重複郵件(看主題),然後刪除郵件。刪除重複的郵件Outlook 2013
此代碼正常工作,但正在刪除最早的重複項。它按降序計數,我似乎無法使項目的排序工作。
基本上我需要幫助搞清楚如何確保按接收時間重複的「最新」重複刪除。
Sub RemoveDuplicates()
Dim oFolder As Folder
Dim oEmail As MailItem, oItems As ItemProperties, oItem As ItemProperty
Dim cMail As Collection
Dim i As Long
Set oFolder = Application.ActiveExplorer.CurrentFolder
Set cMail = New Collection
With oFolder
' .Items.Sort "[ReceivedTime]", True
If olMailItem <> .DefaultItemType Then Exit Sub
For i = .Items.Count To 1 Step -1
Set oItems = .Items(i).ItemProperties
Debug.Print oItems("ReceivedTime")
If Not oItems("ReceivedTime") Is Nothing Then
Set oItem = oItems("ReceivedTime")
'// Week old
If oItem >= Date - 7 Then
On Error GoTo ErrHandler
'// Delete Duplicate Subject
cMail.Add oItems("Subject"), oItems("Subject")
On Error GoTo 0
End If
End If
Next i
End With
Exit Sub
ErrHandler:
Debug.Print Err.Number, oItems("Subject"), oItems("ReceivedTime")
oFolder.Items(i).Delete
Resume Next
End Sub
感謝工作就像一個魅力! Scripting.Dictionary對於其他一些宏將很方便:) – user3665785
當他發佈他的答案時,我已經準備好了,我不想扔掉它。注意'TypeOf'檢查和從'Items(i)'(它是'Object')轉換爲'MailItem'的顯式類型,這使得IntelliSense可以用於VBA IDE中的'EMail'變量。你也可以做'Objects(i).Subject',但是你不會自動完成。 – Tomalak
當使用它作爲郵件Sub RemoveDuplicates(電子郵件爲Outlook.MailItem)時,它不包括觸發腳本的收到的電子郵件。假設我必須創建一個單獨的事件處理程序 – user3665785