2017-07-19 81 views
0

我有一段代碼可以刪除特定文件夾中的所有電子郵件,但我只需要它刪除超過90天的電子郵件,並希望有人將能夠幫助我,我在下面列出了代碼;Outlook VBA,如何刪除90天以前的電子郵件

Sub RemoveAllItemsAndFoldersInDeletedItems() 
    Dim oDeletedItems As Outlook.Folder 
    Dim oFolders As Outlook.Folders 
    Dim oItems As Outlook.Items 
    Dim i As Long 
    'Obtain a reference to deleted items folder 
    Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDrafts) 
    Set oItems = oDeletedItems.Items 
    For i = oItems.Count To 1 Step -1 
     oItems.Item(i).Delete 
    Next 
    Set oFolders = oDeletedItems.Folders 
    For i = oFolders.Count To 1 Step -1 
     oFolders.Item(i).Delete 
    Next 
End Sub 
+1

你可以只設立一個收件箱規則來做到這一點。 – braX

+0

是的,但我被要求設置一個帶有宏的按鈕來做到這一點? –

回答

1

您可以添加一個If語句來比較當前日期和循環中每個電子郵件的發送日期。

更改oItems For循環如下:

For i = oItems.Count To 1 Step -1 
    If DateDiff("d", oItems.Item(i).SentOn, Now) > 90 Then 
     oItems.Item(i).Delete 
    End If 
Next 
+0

謝謝,這就是運行完美。 –

相關問題