VBA: Create a group of Year and Month in Outlook

  • Create a New Colum which name "GroupY" and "GroupYM"

  • Open the Microsoft Visual Basic for Applications from "Developer" tab

  • Insert > Module

  • Use VBA macro into the module:

Sub ListSelectionYearAndMonth()
    Dim aObj As Object
    Dim oProp As Outlook.UserProperty
    Dim sMonth
    Dim sYear
    
    On Error Resume Next
    
    For Each aObj In Application.ActiveExplorer.Selection
        Set oMail = aObj
        
        sMonth = Month(oMail.ReceivedTime)
        sYear = Year(oMail.ReceivedTime)
        Set oProp = oMail.UserProperties.Add("GroupY", olText, True)
        oProp.Value = sYear
        Set oProp = oMail.UserProperties.Add("GroupYM", olText, True)
        oProp.Value = Join(Array(sYear, "-", sMonth))
        
        oMail.Save
        
        Err.Clear
    Next
    
End Sub

Last updated