用于在 x 天后删除电子邮件的 VBA 代码 [英] VBA code to delete emails after x Days

查看:60
本文介绍了用于在 x 天后删除电子邮件的 VBA 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

推荐答案

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

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.

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

这篇关于用于在 x 天后删除电子邮件的 VBA 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆