-->

VBA代码删除X天之后电子邮件(VBA code to delete emails after x

2019-08-17 17:52发布

我想删除我的收件箱是90天前的所有邮件。 我不能使用自动存档,因为它已经在我的办公室被禁用。 我有一些代码,似乎并没有被删除每个邮件是超过90天。 我认为这个问题可能与我的循环。 我使用Outlook 2010与Exchange 2010。

Private Sub RemoveEmail90()

Dim olSession As Outlook.Application, olNamespace As NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim i As Integer
Set olSession = New Outlook.Application
Set olNamespace = olSession.GetNamespace("MAPI")
Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox)
Set Delete_Items = olInbox.Items

For i = Delete_Items.Count To 1 Step -1
    If TypeName(Delete_Items.Item(i)) = "MailItem" Then
            If DateDiff("d",now, Delete_Items.Item(i).ReceivedTime) > 90 Then Delete_Items.Item(i).Delete
    End If
Next

Set olSession = Nothing
Set olNamespace = Nothing
Set olInbox = Nothing
End Sub

Answer 1:

我能够通过调整代码来解决它。 现在的代码运行就好了。 我改变第13行的“M”到“d”,现在它删除所有旧的电子邮件。 更新的代码之上。

If DateDiff("d",now, Delete_Items.Item(i).ReceivedTime) > 90 Then Delete_Items.Item(i).Delete


文章来源: VBA code to delete emails after x Days