删除Outlook中超过一天的文件夹中的项目

时间:2019-05-09 17:55:02

标签: excel vba outlook

我要删除Outlook中文件夹DSP Reports中超过1天的项目,并要求我使用vba代码执行此任务。

现在,代码运行顺畅,没有错误,但是没有删除任何东西,我在运行代码后的昨天和今天都收到了邮件。我使用 F8 进行故障排除,但仍然没有错误。

Sub DSP_Report_Deletion()
'''''''''''''''''''''''''''''''''''''''''''''
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim oItems As Outlook.Items
Dim i

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("DSP Reports")
Set oItems = olFolder.Items


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

'tidy up Outlook
Set olFolder = Nothing
Set oNs = Nothing
Set oOutlook = Nothing
End Sub

enter image description here

1 个答案:

答案 0 :(得分:-1)

Sub DSP_Report_Deletion()
'''''''''''''''''''''''''''''''''''''''''''''
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim oItems As Outlook.Items
Dim i

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("DSP Reports")
Set oItems = olFolder.Items


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

'tidy up Outlook
Set olFolder = Nothing
Set oNs = Nothing
Set oOutlook = Nothing
End Sub