This article will teach you to utilize Outlook VBA to automatically archive the oldest email to a specific PST file as soon as a new email arrives.
Outlook comes endowed with an “Auto Archive” feature. You can make use of it to automatically delete the emails older than a specific date range and move them to a specific archive PST file. However, you can only schedule it to run at specific interval, like 1 day, 1 month, etc., instead of as soon as new email lands into your mailbox. But many users desire that they can configure Outlook to auto archive the oldest email to a specific PST file when new email arrives in the inbox. In this case, the unique way is to apply Outlook VBA. The followings are elaborate steps and VBA codes.
Auto Archive the Oldest Email to a Specific PST File as a New Email Arrives
- In the first place, launch your Outlook application.
- Then you should ensure that the specific PST file which you intend to archive the oldest email is opened and located in the navigation pane.
- Next press “Alt + F11” key shortcuts to get access to Outlook VBA editor,
- In the following window, you need to open the “ThisOutlookSession”.
- Subsequently, copy the VBA codes below into “ThisOutlookSession” project window.
Public WithEvents objNameSpace As Outlook.NameSpace Public WithEvents objInboxItems As Outlook.Items Private Sub Application_Startup() Set objNameSpace = Outlook.Application.GetNamespace("MAPI") Set objInboxItems = objNameSpace.GetDefaultFolder(olFolderInbox).Items End Sub Private Sub objInboxItems_ItemAdd(ByVal Item As Object) Dim objOldestEmail As Outlook.MailItem Dim objTargetFolder As Outlook.MAPIFolder objInboxItems.Sort "[ReceivedTime]", True Set objOldestEmail = objInboxItems.GetLast If TypeOf objOldestEmail Is MailItem Then 'Specify the "Inbox" folder in "Archives" PST file 'You can change the folder, the PST file as per your needs Set objTargetFolder = objNameSpace.Folders("Archives").Folders("Inbox") 'If you want to select folder manually 'use "Set objTargetFolder = objNameSpace.PickFolder objOldestEmail.Move objTargetFolder End If End Sub
- After that, you should sign this code.
- Firstly, create a digital certificate via Digital Certificates for VBA Projects.
- Then assign the new certificate to the new VBA project.
- Later you can change your Outlook macro settings to only allow the digitally signed macros.
- Finally you can restart your Outlook to activate the new macro.
From now on, every time a new email arrives in your Inbox folder, the oldest email in the Inbox will be automatically moved to the “Inbox” folder in “Archives” PST file.
Get Rid of Annoying PST Troubles
As we all know, PST file is the same vulnerable as other common files, like Word documents, Excel workbooks and so on. Therefore, it is an arduous task to keep your PST data well-protected. One of the most important missions is to back up your PST file regularly. Moreover, in order to be able to provide immediate rescue as soon as PST gets corrupted, you should also prepare a remarkable repair tool in vicinity, such as DataNumen Outlook Repair, which can recover Outlook data in quick time.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including SQL Server recovery and outlook repair software products. For more information visit www.datanumen.com