VBOffice

Sort Contacts by Birthday

This script creates a birthday list correctly sorted by day and month, and ignoring the year of birth.

Last modified: 2013/02/01 | Accessed: 34.397  | #96
◀ Previous sample Next sample ▶
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

Contacts in Outlook cannot be usefully sorted by birthday because the year isn't ignored. However, for birthdays only the day and the month are of interest.

This VBA procedure adds a new field to a contact folder of your choice, and copies the day and the month of the contact's birthday to it. For getting it sorted right, the format must be 'mm/dd.', that is the month first, then the day.


tip  How to add macros to Outlook
Public Sub AddFormattedBirthday()
  Dim Folder As Outlook.MAPIFolder
  Dim Items As Outlook.Items
  Dim UserProps As Outlook.UserProperties
  Dim Prop As Outlook.UserProperty
  Dim obj As Object
  Dim Contact As Outlook.ContactItem
  Dim BDay As Date
  Dim DateFormat$
  Dim Name$

  'Name of the new field
  Name = "FormattedBirthday"

  'Date format.
  DateFormat = "mm.dd."

  While Folder Is Nothing
    Set Folder = Application.Session.PickFolder
    If Folder Is Nothing Then Exit Sub
    If Folder.DefaultItemType <> olContactItem Then
      MsgBox "Select a contact folder", vbInformation
      Set Folder = Nothing
    End If
  Wend

  Set Items = Folder.Items
  If Items.Count Then
    For Each obj In Items
      If TypeOf obj Is Outlook.ContactItem Then
        Set Contact = obj
        BDay = Contact.Birthday
        If Year(BDay) > 0 And Year(BDay) < 4000 Then
          Set UserProps = Contact.UserProperties
          Set Prop = UserProps.Item(Name)
          If Prop Is Nothing Then
            Set Prop = UserProps.Add(Name, olText, True)
          End If
          Prop.Value = Format(BDay, DateFormat)
          Contact.Save
        End If
      End If
    Next
  End If
End Sub

Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message