How to Quickly Remove Attachments from Emails Older Than a Specific Date with Outlook VBA

So as to keep Outlook mailbox in small size and prevent Outlook data corruption, it is suggested to remove attachments from the emails which were received or sent a specific period ago. This article will help you quickly accomplish it with Outlook VBA.

As we all know, with Outlook mailbox larger, Outlook data will become much more vulnerable. Therefore, I get used to removing the attachments in the aged emails which are older than a specific date, since that I usually have saved these attachments to my local disk before. But it is a bit troublesome that I have to find out what emails are aged and then manually remove the attachments. In order to improve my efficiency, I come up with a solution by means of Outlook VBA.

Remove Attachments from Received Emails Older Than a Specific Date

  1. To begin with, open Outlook and turn to “Developer” tab.
  2. Then press the “Visual Basic” button in the “Code” group.Visual Basic Button
  3. A new window will turn up. In it, you should open a new module and copy the following codes into it.
Sub RemoveAttachmentsfromAgedEmail()
    Dim olInbox As Outlook.Folder
    Dim varItem As Variant
    Dim i As Integer
    Dim intDatDiff As Integer
    Dim Att As Attachment
 
    Set olInbox = Session.GetDefaultFolder(olFolderInbox)

    For i = olInbox.Items.Count To 1 Step -1
        Set varItem = olInbox.Items.Item(i)
        If varItem.Class = olMail Then
           'calculate the period between now and the received time 
           intDatDiff = DateDiff("d", varItem.ReceivedTime, Now)
           'You can replace "50" as per your needs
           If intDatDiff > 50 Then
              For Each Att In varItem.Attachments
                  Att.Delete
              Next Att
              varItem.Save
           End If
        End If
    Next
End Sub

The Code Aimed to Remove Attachments

  1. Now you can exit the current “Visual Basic” window and proceed to add the macro to Quick Access Toolbar. Follow the steps below:
  • Firstly, hit the down arrow in the Quick Access Toolbar and choose “More Commands” from the drop down list.
  • Then you should select “Macros” in “Choose commands from” field.
  • Next select the correct macro and click “Add” button in center.Add the New Macro to Quick Access Toolbar
  • Lastly, click “OK” button to enable it and back to Outlook main window.
  1. Eventually you can find and click on the button in Quick Access Toolbar. The attachments in the mails, which are older than your specified date, will get removed quickly.Remove Attachments from Aged Emails by VBA

Remove Attachments from Sent Emails Older Than a Specific Date

To achieve this aim, you can copy the whole VBA codes above but change some certain lines by following the notices below:

  1. Firstly, change all the “olInbox” to “olSentItemFolder” in the VBA codes.
  2. Then replace “Set olInbox = Session.GetDefaultFolder(olFolderInbox)” with:
Set olSentItemFolder = Session.GetDefaultFolder(olFolderSentMail)
  1. Next replace “intDatDiff = DateDiff(“d”, varItem.ReceivedTime, Now)” with:
intDatDiff = DateDiff("d", varItem.SentOn, Now)

Eliminate Annoying Outlook Errors

If you’ve used Outlook for a long time, frequent errors must have become common matters. It is apparent that you cannot predict or completely get rid of them. What you can do is just to try your best to resolve them. For example, use the inbox repair tool, Scanpest.exe to fix Outlook email error. Also, you have to make a regular backup for your Outlook data in case of unanticipated crash.

Author Introduction:

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

Leave a Reply

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