VBA在特定时间段内从用户创建的文件夹中删除Outlook邮件

时间:2010-05-26 19:24:28

标签: vba email outlook

我正在寻找一个VBA代码段来删除六(6)个月或更长时间的Outlook邮件并位于用户创建的文件夹中。

该代码是什么样的?

1 个答案:

答案 0 :(得分:5)

这是一个示例脚本

Sub DeleteOlderThan6months()

Dim oFolder As Folder
Dim Date6months As Date
Dim ItemsOverMonths As Outlook.Items

Dim DateToCheck As String

Date6months = DateAdd("d", -182, Now())
Date6months = Format(Date6months, "mm/dd/yyyy")


Set oFolder = Application.Session.PickFolder 'or set your folder

DateToCheck = "[Received] <= """ & Date6months & """"

Set ItemsOverMonths = oFolder.Items.Restrict(DateToCheck)

For i = ItemsOverMonths.Count To 1 Step -1
    ItemsOverMonths.Item(i).Delete
Next


Set ItemsOverMonths = Nothing
Set oFolder = Nothing


End Sub