How to Auto Delete the Outlook Emails from a Specific Sender after Specific Days

To let your Outlook auto delete the emails from a specific sender from specific days, you can read this article. Here we will show you the detailed steps to use VBA code to get it.

When it comes to auto deleting emails, you may firstly think of the “AutoArchive” feature. However, it cannot filter the specific senders when auto archiving emails. Therefore, for instance, if you would like to auto delete the emails from a specific sender after x days, you should use the other means, such as the following one.

Auto Delete the Emails from a Specific Sender after X Days

  1. At the very outset, start your Outlook application.
  2. Then, trigger Outlook VBA editor according to “How to Run VBA Code in Your Outlook“.
  3. Next, copy the following VBA code into “ThisOutlookSession” project.
Public WithEvents objInboxItems As Outlook.Items

Private Sub Application_Startup()
    Set objInboxItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
 
    Call DeleteEmailsFromSpecificSenderAfterXDays
End Sub

Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim objMail As Outlook.MailItem
 
    If TypeOf Item Is MailItem Then
       Set objMail = Item
 
       'From the specific sender
       If objMail.SenderEmailAddress = "bob_black@datanumen.com" Then
          'Set expiry time - after 5 days
          objMail.ExpiryTime = objMail.ReceivedTime + 4
          objMail.Save
       End If
    End If
End Sub

Private Sub DeleteEmailsFromSpecificSenderAfterXDays()
    Dim strFilter As String
    Dim objExpiredItems As Outlook.Items
    Dim objExpiredMail As Outlook.MailItem
 
    strFilter = "[ExpiryTime] <= " & Chr(34) & Now & Chr(34)
 
    'Get all expired items
    Set objExpiredItems = objInboxItems.Restrict(strFilter)
 
    For i = objExpiredItems.Count To 1 Step -1
        If objExpiredItems(i).Class = olMail Then
           Set objExpiredMail = objExpiredItems(i)
 
           'Auto delete expired emails from the specific sender
           If objExpiredMail.SenderEmailAddress = "bob_black@datanumen.com" Then
              objExpiredMail.Delete
           End If
        End If
    Next
End Sub

VBA Code - Auto Delete the Emails from a Specific Sender after Specific Days

  1. After that, restart Outlook to activate this macro.
  2. Since then, every time when a new email from the specific sender arrives in Inbox, it will be assigned with a specific expiry time – the specific days after it is received.Auto Added Expiry Time
  3. Then, every time you start Outlook, Outlook will auto check and delete the expired mails from the specific sender.Auto Deleted Expired Emails from a Specific Sender

Restore Outlook Data after Corruption

Perhaps you have encountered a variety of errors and troubles in Outlook. Then, have you ever confronted Outlook corruption? For example, if you frequently exit Outlook improperly, your PST file tends to get damaged. Generally, in such a case, you can select to retrieve Outlook data from backups. Or you can either use inbox repair tool or a reliable external tool, such as DataNumen Outlook Repair.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover sql and outlook repair software products. For more information visit www.datanumen.com

One response to “How to Auto Delete the Outlook Emails from a Specific Sender after Specific Days”

  1. Hello , i read Your post but your Code did not work,
    Please Help, I’m a beginner in VBA Outlook,

    Can I have the full file to run it correctly.

    Thank you

    “” How to Get a Notification If Not Receiving the Reply of a Specific Email within Expected Time “”

    Public WithEvents objInboxItems As Outlook.Items

    Private Sub Application_Startup()
    Set objInboxItems = Application.Session.GetDefaultFolder(olFolderInbox).Items
    End Sub

    ‘If receive the reply, clear the flag and remove the reminder
    Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim objSentItems As Outlook.Items
    Dim objVariant As Variant
    Dim i As Long
    Dim strSubject As String
    Dim dSendTime As String

    Set objSentItems = Outlook.Application.Session.GetDefaultFolder(olFolderSentMail).Items

    If Item.Class = olMail Then
    For i = 1 To objSentItems.Count
    If objSentItems.Item(i).Class = olMail Then
    Set objVariant = objSentItems.Item(i)
    strSubject = LCase(objVariant.Subject)
    dSendTime = objVariant.SentOn

    If LCase(Item.Subject) = “re: ” & strSubject Or InStr(LCase(Item.Subject), strSubject) > 0 Then
    If Item.SentOn > dSendTime Then
    With objVariant
    .ClearTaskFlag
    .ReminderSet = False
    .Save
    End With
    End If
    End If
    End If
    Next i
    End If
    End Sub

    ‘Get a prompt asking if to send a notification email
    Private Sub Application_Reminder(ByVal Item As Object)
    Dim strPrompt As String
    Dim nResponse As Integer
    Dim objFollowUpMail As Outlook.MailItem

    ‘You can change the subject as per your real case
    If (Item.Class = olMail) And (LCase(Item.Subject) = “datanumen outlook repair”) Then
    strPrompt = “You haven’t yet recieved the reply of ” & Chr(34) & Item.Subject & Chr(34) & ” within your expected time. Do you want to send a follow-up notification email?”
    nResponse = MsgBox(strPrompt, vbYesNo + vbQuestion, “Confirm to Send a Follow-Up Notification Email”)
    If nResponse = vbYes Then
    Set objFollowUpMail = Application.CreateItem(olMailItem)
    With objFollowUpMail
    .To = Item.Recipients.Item(1).Address
    .Subject = “Follow Up: ” & Chr(34) & Item.Subject & Chr(34)
    .Body = “Please respond to my email ” & Chr(34) & Item.Subject & Chr(34) & “as soon as possible”
    .attachments.Add Item
    .Display
    End With
    End If
    End If
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *