If you would like to let Outlook auto delete specific mails, you can set an expiry time for the emails and use AutoArchive to delete the expired emails when the time is up. This article will teach you how to auto set an expiry time for all incoming emails via Outlook VBA.
In general, AutoArchive can work on either a whole folder or an entire mailbox. If you want to enable it to delete specific items only, you will need to firstly specify the expiry time for the emails. Thereby, the next time when the AutoArchive runs, it will automatically delete the expired time. In addition, as you can see, the mails with an expiry time will display in grey strikethrough font, like the image below.
You can set the expiry time for emails in the current email Properties window. By this means, you have to specify it manually every time, which is cumbersome.
Under this circumstance, you must wish that Outlook can auto set the expiry time for incoming or outgoing emails. You can accomplish it with Outlook VBA. Here are the elaborate steps and codes.
Auto Set an Expiry Time for Incoming or Outgoing Emails
- In the first place, launch Outlook application and shift to “Developer” tab.
- Then click “Visual Basic” button.
- In the new popup window, open “ThisOutlookSession” VBA project and then copy and paste the following codes into it.
Public WithEvents olItems As Items Private Sub Application_Startup() 'To focus on the new incoming emails Set olItems = Application.Session.GetDefaultFolder(olFolderInbox).Items 'If want to focus on the outgoing emails 'Use "Set olItems = Application.Session.GetDefaultFolder(olFolderSentMail).Items End Sub Private Sub olItems_ItemAdd(ByVal Item As Object) Dim strMsg As String Dim nRes As Integer If Item.ExpiryTime = #1/1/4501# Then '("m",2,Item.ReceivedTime) refers to 2 months after the item arrives in the specified folder 'You can change it as per your actual needs Item.ExpiryTime = DateAdd("m", 2, Item.ReceivedTime) strMsg = "The new email " & Chr(34) & Item.Subject & Chr(34) & "will expire on " & DateAdd("m", 2, Item.ReceivedTime) & "." nRes = MsgBox(strMsg, vbExclamation + vbOKOnly, "Expiry Time") End If Item.Save End Sub
- After that, you ought to digitally sign the new “ThisOutlookSession” macro.
- Subsequently, you need to change your Outlook macro security level to low.
- Finally restart Outlook to activate the VBA project. From now on, all the new emails in Inbox or Sent Items folder will have the expiry time. Also, you will receive a message box like the following screenshot.
Keep Malicious Emails at Bay
Outlook cannot be exempt from corruptions in that nowadays malicious emails have become one of the most common security threats. Viruses can be hidden in the emails easily. Therefore, you should keep cautious all the time in dealing with new emails. Otherwise, it is very likely that you will suffer from Outlook PST file corruption, which will be rather knotty.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair corrupted SQL Server mdf database and outlook repair software products. For more information visit www.datanumen.com