This post will teach you how to let Outlook auto archive the old items in your Inbox folder as soon as the total count of Inbox items exceeds a specific limit.
As we all know, Outlook offers users with an “AutoArchive” feature. However, by default, it can only be configured to run based on the predefined time interval. If you want to let it auto run depending on the total number of items in an Outlook folder, you have no choice but to utilize other means, such as VBA. Thus, in the followings, we will expose such a piece of VBA code to you.
Auto Archive Old Items in Inbox When It is Too Full
- To start with, launch your Outlook application.
- Then in the Outlook main screen, press “Alt + F11” key shortcuts to access its VBA editor.
- After entering the “Microsoft Visual Basic for Applications” window, you can find and open the “ThisOutlookSession” project.
- Subsequently, you need to copy the following VBA code into this project.
Public WithEvents objInboxItems As Outlook.Items Private Sub Application_Startup() Set objInboxItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items End Sub Private Sub objInboxItems_ItemAdd(ByVal Item As Object) Call AutoArchive_InboxTooFull End Sub Sub AutoArchive_InboxTooFull() Dim objInbox As Outlook.Folder Dim objArchivePSTFile As Outlook.Folder Dim objArchiveFolder As Outlook.Folder Dim i As Long Dim objVariant As Variant Dim lArchivedItems As Long Dim nDateDiff As Integer Set objInbox = Application.Session.GetDefaultFolder(olFolderInbox) 'Open the specific Archive Outlook PST file in your Outlook Application.Session.AddStore "C:\Users\Test\Documents\Outlook Files\Archive.pst" Set objArchivePSTFile = Application.Session.folders("Archives") Set objArchiveFolder = objArchivePSTFile.folders("Inbox") 'Change the "100" to the maximum number of Inbox items as you want If objInbox.Items.count > 100 Then For i = objInbox.Items.count To 1 Step -1 Set objVariant = objInbox.Items.Item(i) DoEvents If objVariant.Class = olMail Then 'Here archive the Inbox items older than 7 days 'You can change it as per your needs, such as: 'nDateDiff = DateDiff("m", objVariant.SentOn, Now) 'If nDateDiff > 2 Then ==> items older than 2 months nDateDiff = DateDiff("d", objVariant.SentOn, Now) If nDateDiff > 7 Then objVariant.Move objArchiveFolder lArchivedItems = lArchivedItems + 1 End If End If Next i 'Display the number of items that were archived. MsgBox "Archived " & lArchivedItems & " email(s).", vbInformation + vbOKOnly End If 'Remove the Archive PST file from your Outlook Application.Session.RemoveStore objArchivePSTFile End Sub
- After that, you should sign this code. Just click “Tools” > “Digital Signatures” and then follow the onscreen instructions.
- Later change your Outlook macro settings to allow digitally signed macros.
- Eventually, you can restart your Outlook to activate the new VBA project.
- From now on, every time an email arrives in Inbox, Outlook will auto count the Inbox items. If the total count surpasses a specific limit, Outlook will then archive the items older than a predefined time to a specified archive PST file.
Extract Data from Compromised Outlook File
Though Outlook earns a lot of kudos due to its numerous features, it still cannot eliminate its vulnerabilities. Therefore, so as to avoid painful Outlook data loss or corruption, it’s recommended to make regular backups. Moreover, if possible, you are better off keeping a preeminent PST repair tool nearby, such as DataNumen Outlook Repair. It is suggested due to its high recovery rate.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover Sql Server and outlook repair software products. For more information visit www.datanumen.com