我有一段代码删除了特定文件夹中的所有电子邮件,但我需要它只删除超过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
答案 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