用于在 x 天后删除电子邮件的 VBA 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15482736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:05:59  来源:igfitidea点击:

VBA code to delete emails after x Days

vbaoutlook-vbaoutlook-2010

提问by randomDCnick

I am trying to delete all emails in my inbox that are older than 90 days. I am not able to use the auto archive since it has been disabled at my office. I have some code that does not seem to be deleting every mail that is older than 90 days. I think the issue might be with my loop. I am using Outlook 2010 with exchange 2010.

我正在尝试删除收件箱中超过 90 天的所有电子邮件。我无法使用自动存档,因为它已在我的办公室被禁用。我有一些代码似乎没有删除所有超过 90 天的邮件。我认为问题可能出在我的循环上。我在 Exchange 2010 中使用 Outlook 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

采纳答案by randomDCnick

I was able to fix it by tweaking the code. Now the code runs just fine. I change the "m" on line 13 to a "d" and now it is deleting all older emails. Updated code Above.

我能够通过调整代码来修复它。现在代码运行得很好。我将第 13 行上的“m”更改为“d”,现在它正在删除所有旧电子邮件。更新了上面的代码。

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