How to Auto Classify and Archive Your Emails by Month with Outlook VBA

Some users prefer to classify the received emails by the month of the received time. This article will tell you how to let Outlook auto classify and archive all your emails with VBA.

In order to make your mailbox orderly, you will use various methods, including color categorizing, filing emails on basis of their received time and so on. Without any doubts, managing emails by saving them in different and specific mail folders is a good idea. For instance, as shown in the following screenshot, many people like to classify and archive their emails based on the specific month of the emails’ received time.

Classify and Archive Your Emails by Month

In this case, some users get used to creating such mail folders manually and then move them one by one. Obviously, this approach will be pretty troublesome. Thus, why not recur to Outlook VBA? It can help you achieve it automatically. Here are the detailed steps and concrete VBA codes.

Auto Classify and Archive Your Emails by Month

  1. In the first place, launch Outlook and shift to “Developer” tab. If not find it, go to Outlook Options to activate it.
  2. Then click on the “Visual Basic” button under this tab, which will bring you to the VBA editor.
  3. Next you should find and open the “ThisOutlookSession” project window.
  4. Subsequently, you have to copy the following VBA codes into it.
Public WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
    Set objItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objItems_ItemAdd(ByVal Item As Object)
    Dim strYear As String
    Dim strMonth As String
    Dim objInboxFolder As Folder
    Dim objDestinationFolder As Folder
 
    strYear = Year(Item.ReceivedTime)
    strMonth = Month(Item.ReceivedTime)
 
    Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)
 
    If TypeOf Item Is MailItem Then
       On Error Resume Next
       Set objDestinationFolder = objInboxFolder.Folders(strYear & "." & strMonth)
       If objDestinationFolder Is Nothing Then
          Set objDestinationFolder = objInboxFolder.Folders.Add(strYear & "." & strMonth)
       End If
       Item.Move objDestinationFolder
    End If
End Sub

VBA Codes - Auto Classify and Archive Emails by Month

  1. After that, you ought to digitally sign this code and then change your macro security level to low.
  2. Finally, you can restart Outlook to activate the new macro. From now on, all the incoming emails will be auto filed by month. If there is no corresponding mail folder named as the specific month, Outlook will auto create it and move the new incoming emails into it.

File the Existing Received Emails by Month

If you have already multiple emails in your Inbox folder and now you want to file them by month as well, you can copy the following codes into a new module.

Sub FileExistingEmailsbyMonth()
    Dim objInboxFolder As Folder
    Dim objVariant As Variant
    Dim i As Long
    Dim strYear As String
    Dim strMonth As String
    Dim objDestinationFolder As Folder
 
    Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
 
    For i = objInboxFolder.Items.Count To 1 Step -1
        If TypeOf objInboxFolder.Items.Item(i) Is MailItem Then
           Set objVariant = objInboxFolder.Items.Item(i)
           DoEvents
           strYear = Year(objVariant.ReceivedTime)
           strMonth = Month(objVariant.ReceivedTime)
           On Error Resume Next
           Set objDestinationFolder = objInboxFolder.Folders(strYear & "." & strMonth)
           If objDestinationFolder Is Nothing Then
              Set objDestinationFolder = objInboxFolder.Folders.Add(strYear & "." & strMonth)
           End If
           objVariant.Move objDestinationFolder
       End If
    Next
End Sub

VBA Codes - Archive Existing Received Time by Month

After that, you can directly click the “Run” button in the current window toolbar to launch the new macro. At once, all the existing emails in Inbox will be filed by month.Run the Current Macro

Prevent Outlook Corruption

It is an unquestioned fact that no software is foolproof, surely including Outlook. So in order to protect your Outlook from damage, you have to make lots of efforts. For example, you have to keep mindful of unknown incoming emails which may contain viruses. Moreover, you need to get the habits of always closing Outlook properly. Also, it is necessary for you to ensure your PST file is in small size. Last but not least, you’re required to get hold of an experienced Outlook repair tool, like DataNumen Outlook Repair, which will come in handy.

Author Introduction:

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

One response to “How to Auto Classify and Archive Your Emails by Month with Outlook VBA”