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
- To begin with, open Outlook and turn to “Developer” tab.
- Then press the “Visual Basic” button in the “Code” group.
- 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
- 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.
- Lastly, click “OK” button to enable it and back to Outlook main window.
- 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 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:
- Firstly, change all the “olInbox” to “olSentItemFolder” in the VBA codes.
- Then replace “Set olInbox = Session.GetDefaultFolder(olFolderInbox)” with:
Set olSentItemFolder = Session.GetDefaultFolder(olFolderSentMail)
- 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.
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