Hey, Scripting Guy! How Can I Determine the Age, in Years, of an Office Outlook Contact?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Given the birthday for one of my Office Outlook contacts, how can I tell how old that person is?

— JC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JC. We have to tell you, JC, that you initially gave us quite a start with this question. Why? Well, when the Scripting Guy who writes this column sat down to write this column, he tossed together a script and, in order to test the thing, ran the script against his own set of Microsoft Outlook contacts. Back came the requested names and ages, including the ages of the Scripting Brothers and the Scripting Sister. Holy smokes! Needless to say, the Scripting Guy who writes this column was shocked by what he saw: the Scripting Brothers and the Scripting Sister are all old! And if the Scripting Guy who writes this column is the oldest child in the family that can only mean one thing ….

That’s right: that can only mean that the Scripting Parents made a mistake awhile back and inadvertently told the Scripting Guy who writes this column that he was the oldest child in the family; in reality, it turns out that he must be the youngest child in the family. You know how parents do things like that, always yelling at Billy when they really meant to yell at Betty, always telling a child he’s the oldest in the family when he’s really the youngest in the family, etc., etc. Obviously this was just one of those mistakes that parents are prone to make.

Admittedly, until the Scripting Guy who writes this column realized what had happened he was in shock. But now it all makes sense. Besides, no one could possibly be older than the Scripting Brothers and the Scripting Sister. In fact, the Scripting Guy who writes this column should probably give the old geez – um, give the Scripting Siblings a call and see how they’re doing. And maybe give the Guinness Book of World Records a call while he’s at it. Move over giant tortoise; we’ve found someone even older than you.

Note. For those of you who like to keep track of this sort of thing, Tu’i Malila was the name of a tortoise that, shortly after its birth in 1777, was given to the royal family of Tonga by Captain James Cook. Tu’i died in 1965 at the ripe old age of 188, making it the oldest tortoise ever recorded. In all fairness, however, we should note that there have been disputed claims that a giant tortoise named Adwaita was as old as 256 when he died. If verified, that would make Adwaita the oldest tortoise ever recorded.

Incidentally, it is not true that Scripting Guy Peter Costantini raised Adwaita as a hatchling. In fact, at the time Adwaita was born, Scripting Guy Peter Costantini was attending college and not allowed to have pets in the dorm.

Now, we know that some of you are probably thinking, “That’s the dumbest thing I ever heard: this Scripting Guy’s parents accidentally told him he was the oldest child in the family when he was really the youngest?!?” Admittedly, that is a little hard to believe. But we’ll prove it to you. Obviously no one but a young, vibrant person still in the prime of his life could write a script that, given a birthday, can tell you how old that person is, agreed? Agreed. Then, feast your eyes on this:

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 dtmBirthday = objContact.Birthday intAge = DateDiff(“yyyy”, dtmBirthday, Now) Wscript.Echo objContact.FullName & ” Age: ” & intAge End If

Next

Were you able to feast your eyes on that? Good; the Scripting Guy who writes this column forgot his bifocals this morning and is having problems feasting his eyes on anything. (And yes, as a matter of fact he does seem to forget things more often than he used to. Why do you ask?) As you can see, the script starts out by defining a constant named olFolderContacts and setting the value to 10; we’ll use this constant to tell the script which Outlook folder to connect to. (Obviously the Contacts folder.) We next create an instance of the Outlook.Application object, then bind ourselves to the MAPI namespace; that’s what these two lines of code are for:

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

Once we’re officially connected to both Microsoft Outlook and the MAPI namespace we call the GetDefaultFolder method to retrieve a collection of all the items (contacts) found in the Contacts folder:

Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items

At this point, we’re ready to do some birthday calculating.

To begin with, we set up a For Each loop to loop through all the contacts in the collection. The first thing we do within that loop? We check to see if any of the contacts had a birth date of 1/1/4501:

If objContact.Birthday <> #1/1/4501# Then

Why do we care whether or not someone has a birth date of 1/1/4501? Well, if you don’t list a birthday for a contact Outlook automatically gives that person a default birthday of 1/1/4501. (By remarkable coincidence, that’s the exact same day that the Scripting Guy who writes this column hopes to have saved up enough money so he can retire.) We’re not too-terribly interested in contacts who won’t be born for another 2494 years, so we use our If Then statement to weed out contacts who don’t have a birthday.

Note. So what will happen in the year 4501, when it becomes possible for someone to actually have a birth date of 1/1/4501? We don’t know for sure. But we wouldn’t worry too much about that; no doubt someone from Microsoft is already working on a hot fix that will take care of the problem.

And yes, you probably will still have to reboot your computer after applying that hot fix. Some things never change.

Let’s assume that we’ve encountered a contact who does have a birthday. (Incidentally, if anyone out there doesn’t have a birthday, please let us know. After all, the Scripting Editor has had 87 of them; maybe we could convince her to share one or two with you.) Whenever we run into a user who has a birthday we assign that birth date to a variable named dtmBirthday:

dtmBirthday = objContact.Birthday

That brings us to this line of code:

intAge = DateDiff(“yyyy”, dtmBirthday, Now)

What we’re doing here is calculating the age of this contact, in years. To do that, we use the DateDiff function, passing DateDiff three parameters:

“yyyy”, which tells the script that we want to calculate the age in years. Are there other time intervals we could use when calling the DateDiff function? You bet there are; for more information, see the Microsoft Windows 2000 Scripting Guide.

dtmBirthday, the variable holding the contact’s birthday.

Now, a VBScript function that retrieves the current date and time.

We store the results of the DateDiff calculation in a variable named intAge, then use this line of code to echo back the contact name and their current age, in years:

Wscript.Echo objContact.FullName & ” : ” & intAge

And, in case you’re wondering, yes, this will be the person’s current age. For example, suppose you have a contact who was born on December 19, 1989. At the time this column was written (October 30, 2007) that contact was still 17 years old; he or she hadn’t reached their 18th birthday yet. Fortunately, our script will correctly report back their age as 17. It might not be a big deal in this case, but, trust us, you don’t want to say someone is 40 when, technically, they’re still 39.

Note. Not that the Scripting Guy who writes this column would know that through personal experience. However, he has read about people who have – miraculously – managed to reach the age of 40. And some of these people can be very touchy about the whole subject of how old they are.

Incidentally, here’s the kind of output you can expect to see:

Ken Myer: 35
Pilar Ackerman: 62
Jonathan Haas: 40
Scripting Editor: 24

Note. You can ignore that last line; that age is not what was listed before this column was edited.

And that should do it, JC; let us know if you have any further questions. Oh, and, before you go, do you have time for a long, boring, and completely-pointless story? That’s all right; we understand. Maybe next time. See you all tomorrow.

Note. Believe it or not, that last paragraph was not an attempt to poke fun at senior citizens; the truth is, we really did want to know if JC had time for a long, boring, and completely-pointless story. After all, aren’t long, boring, and completely-pointless stories what Hey, Scripting Guy! is all about?

0 comments

Discussion is closed.

Feedback usabilla icon