How Can I Tell If Any of My Contacts Have a Birthday This Month?

How Can I Tell If Any of My Contacts Have a Birthday This Month?

  • Comments 1
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell if any of my contacts have a birthday this month?

-- VG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, VG. You sly fox, you: how did you know that the Scripting Guy who writes this column is celebrating his birthday?!? And no, you don’t need to send him a present; after all, he writes this column because he loves his work, not because he expects people to shower him with gifts on his birthday. Heaven forbid.

But, if you insist, send your gifts here:

The Scripting Guy Who Writes That Column
Microsoft Corporation
Building 42/4039
One Microsoft Way
Redmond, WA 98052

And yes, cash will be fine. Thanks!

Editor’s Note: Just kidding everyone. It’s not even his birthday (read on). He tries to get free desserts from restaurants like this too.

Of course, now we feel bad that we didn’t get you anything for your birthday, VG. Tell you what; how about a script that can tell you if any of your contacts have a birthday this month? Sound good? Hang on a second; we have to zip over to Scripts ‘R Us. We’ll be right back.

Whew; we got the last one they had in stock. Hope this fits, VG:

On Error Resume Next

Const olFolderContacts = 10

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")

Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items

For Each objContact In colContacts
    If Month(objContact.Birthday) = Month(Date) Then
        Wscript.Echo objContact.FullName, objContact.Birthday
    End If
Next

Before we explain how this script works we should note that the assumption here is that all your contacts are in the same folder. If that’s not the case, that is, if you have subfolders in your main contacts folder, well, then this script will be far from foolproof: it will return information about the contacts in the main folder, but not for those contacts in any subfolders. If you have subfolders then you’ll need to write a recursive function that can access the information found in those subfolders. And how do you do that? Beats the heck out of us. But you can find an example of a recursive function that works with Microsoft Outlook in the Hey, Scripting Guy! archive.

As for the script itself, we start things off with the On Error Resume Next statement. That’s something we typically don’t use in our scripts, but we occasionally encountered an error when dealing with contact birthdays. We’re not totally sure why we got the error, but, seeing as how On Error Resume Next took care of things, we decided to just accept things as they were and not worry too much about it. If we ever go back and look into this a little closer we’ll let you know.

But don’t hold your breath waiting.

After enabling error handling we define a constant named olFolderContacts; we’ll use this constant to tell the script which Outlook folder we want to work with. We then use these two lines of code to create an instance of the Outlook.Application object and to bind to the MAPI namespace:

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")

We should also point out that this script assumes that Outlook is already up and running. What are you supposed to do if Outlook isn’t already up and running? Beats the heck out of us. But you can find an example of a script that determines whether or not Outlook is already running (and, if not, start it) in the Office Space archive.

Now, in theory, we could probably write a complicated filter that would automatically weed out contacts that don’t have a birthday this month. To tell you the truth, however, that seemed like more trouble than it was worth; as near as we could tell it was just as fast (and way easier) to individually check each contact’s birthday. Therefore, our next step is to use this line of code to retrieve a collection of all the contacts in the Contacts folder:

Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items

What are we going to do with that collection? Funny you should ask. To begin with, we’re going to set up a For Each loop to walk through the entire collection. For each contact in that collection we’ll use this line of code to determine whether the contact has a birthday in the current month:

If Month(objContact.Birthday) = Month(Date) Then

All we’re doing here is using the command Month(objContact.Birthday) to determine the numeric value of the month in which the contact was born (1 for January, 2 for February, 3 for March, etc.). We then compare that with the current month; that’s what Month(Date) tells us. If the values match then the contact has a birthday this month. In turn, we echo back the contact name and birthday:

Wscript.Echo objContact.FullName, objContact.Birthday

If the two months don’t match then we simply loop around and check the next contact in the collection.

When we’re all done we should get back a report similar to this (assuming that the script is run sometime in March):

Jonathan Haas 3/28/1977
Ken Myer 3/21/1938
Pilar Ackerman 3/14/1968

Note. Well, what do you know: Ken Myer and Scripting Guy Jean Ross were born on the exact same day in the exact same year! Who would have guessed it!?!

One thing to watch out for here. If you haven’t specified a birthday for a contact Outlook automatically assigns that person a birthday of 1/1/4501. (Interestingly, that’s the same year that we expect Scripting Guy Jean Ross to finally stop complaining about our good-natured jab about her age. [Editor’s Note: Actually, Jean thinks it wouldn’t be all bad for people to believe she was born in 1938; she’d get senior discounts, and a lot of comments on how great she looks for her age – not that she doesn’t get those comments anyway.]) What that means is that, as far as this script is concerned, anyone without a designated birthday will appear to have a birthday in January. Unless, of course, you run this modified script, which simply ignores anyone with a birthday of 1/1/4501:

On Error Resume Next

Const olFolderContacts = 10

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")

Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items

For Each objContact In colContacts    
    If objContact.Birthday <> #1/1/4501# Then
        If Month(objContact.Birthday) = Month(Date) Then
            Wscript.Echo objContact.FullName, objContact.Birthday
        End If
    End If
Next

OK, we need to come clean with you here: the Scripting Guy who writes this column had his birthday on December 18th, a birthday he shares with baseball immortal Ty Cobb and actor Brad Pitt. (The Scripting Guy who writes this column likes to think he combines the traits of both of these legends: he looks exactly like Ty Cobb and plays baseball exactly like Brad Pitt.) Does that mean that you shouldn’t send him a birthday present after all? Well, you know, it’s never too early to get a jump on his next birthday ….

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment