{"id":63693,"date":"2007-10-31T00:08:00","date_gmt":"2007-10-31T00:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/31\/hey-scripting-guy-how-can-i-determine-the-age-in-years-of-an-office-outlook-contact\/"},"modified":"2007-10-31T00:08:00","modified_gmt":"2007-10-31T00:08:00","slug":"hey-scripting-guy-how-can-i-determine-the-age-in-years-of-an-office-outlook-contact","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-determine-the-age-in-years-of-an-office-outlook-contact\/","title":{"rendered":"Hey, Scripting Guy! How Can I Determine the Age, in Years, of an Office Outlook Contact?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! Given the birthday for one of my Office Outlook contacts, how can I tell how old that person is?<BR><BR>&#8212; JC<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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 <I>old<\/I>! And if the Scripting Guy who writes this column is the oldest child in the family that can only mean one thing \u2026.<\/P>\n<P>That\u2019s 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 <I>youngest<\/I> 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\u2019s the oldest in the family when he\u2019s really the youngest in the family, etc., etc. Obviously this was just one of those mistakes that parents are prone to make.<\/P>\n<P>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 <I>possibly<\/I> 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 \u2013 um, give the Scripting Siblings a call and see how they\u2019re doing. And maybe give the Guinness Book of World Records a call while he\u2019s at it. Move over giant tortoise; we\u2019ve found someone even older than <I>you<\/I>.<\/P>\n<TABLE id=\"EMD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. For those of you who like to keep track of this sort of thing, Tu&#8217;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\u2019i 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.<\/P>\n<P>Incidentally, it is <I>not<\/I> 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, we know that some of you are probably thinking, \u201cThat\u2019s the dumbest thing I ever heard: this Scripting Guy\u2019s parents accidentally told him he was the <I>oldest<\/I> child in the family when he was really the <I>youngest<\/I>?!?\u201d Admittedly, that <I>is<\/I> a little hard to believe. But we\u2019ll 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 <I>this<\/I>:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const olFolderContacts = 10<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)<\/p>\n<p>Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items<\/p>\n<p>For Each objContact In colContacts    \n    If objContact.Birthday &lt;&gt; #1\/1\/4501# Then\n        dtmBirthday = objContact.Birthday\n        intAge = DateDiff(&#8220;yyyy&#8221;, dtmBirthday, Now)\n        Wscript.Echo objContact.FullName &amp; &#8221; Age: &#8221; &amp; intAge\n    End If<\/p>\n<p>Next\n<\/PRE>\n<P>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 <I>does<\/I> seem to forget things more often than he used to. Why do you ask?) As <I>you<\/I> can see, the script starts out by defining a constant named olFolderContacts and setting the value to 10; we\u2019ll use this constant to tell the script which Outlook folder to connect to. (Obviously the Contacts folder.) We next create an instance of the <B>Outlook.Application<\/B> object, then bind ourselves to the MAPI namespace; that\u2019s what these two lines of code are for:<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\n<\/PRE>\n<P>Once we\u2019re officially connected to both Microsoft Outlook and the MAPI namespace we call the <B>GetDefaultFolder<\/B> method to retrieve a collection of all the items (contacts) found in the Contacts folder:<\/P><PRE class=\"codeSample\">Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items\n<\/PRE>\n<P>At this point, we\u2019re ready to do some birthday calculating. <\/P>\n<P>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:<\/P><PRE class=\"codeSample\">If objContact.Birthday &lt;&gt; #1\/1\/4501# Then\n<\/PRE>\n<P>Why do we care whether or not someone has a birth date of 1\/1\/4501? Well, if you don\u2019t list a birthday for a contact Outlook automatically gives that person a default birthday of 1\/1\/4501. (By remarkable coincidence, that\u2019s the exact same day that the Scripting Guy who writes this column hopes to have saved up enough money so he can retire.) We\u2019re not too-terribly interested in contacts who won\u2019t be born for another 2494 years, so we use our If Then statement to weed out contacts who don\u2019t have a birthday.<\/P>\n<TABLE id=\"EBF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. So what will happen in the year 4501, when it becomes possible for someone to actually <I>have<\/I> a birth date of 1\/1\/4501? We don\u2019t know for sure. But we wouldn\u2019t worry too much about that; no doubt someone from Microsoft is already working on a hot fix that will take care of the problem.<\/P>\n<P>And yes, you probably <I>will<\/I> still have to reboot your computer after applying that hot fix. Some things <I>never<\/I> change.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s assume that we\u2019ve encountered a contact who <I>does<\/I> have a birthday. (Incidentally, if anyone out there <I>doesn\u2019t<\/I> 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:<\/P><PRE class=\"codeSample\">dtmBirthday = objContact.Birthday\n<\/PRE>\n<P>That brings us to this line of code:<\/P><PRE class=\"codeSample\">intAge = DateDiff(&#8220;yyyy&#8221;, dtmBirthday, Now)\n<\/PRE>\n<P>What we\u2019re doing here is calculating the age of this contact, in years. To do that, we use the <B>DateDiff<\/B> function, passing DateDiff three parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>&#8220;yyyy&#8221;<\/B>, 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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_vbs_ufbq.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>dtmBirthday<\/B>, the variable holding the contact\u2019s birthday.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Now<\/B>, a VBScript function that retrieves the current date and time.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>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:<\/P><PRE class=\"codeSample\">Wscript.Echo objContact.FullName &amp; &#8221; : &#8221; &amp; intAge\n<\/PRE>\n<P>And, in case you\u2019re wondering, yes, this will be the person\u2019s <I>current<\/I> 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\u2019t reached their 18<SUP>th<\/SUP> 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\u2019t want to say someone is 40 when, technically, they\u2019re still 39.<\/P>\n<TABLE id=\"EIH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Not that the Scripting Guy who writes this column would know that through personal experience. However, he <I>has<\/I> read about people who have \u2013 miraculously \u2013 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Incidentally, here\u2019s the kind of output you can expect to see: <\/P><PRE class=\"codeSample\">Ken Myer: 35\nPilar Ackerman: 62\nJonathan Haas: 40\nScripting Editor: 24\n<\/PRE>\n<TABLE id=\"EYH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. You can ignore that last line; that age is <I>not<\/I> what was listed before this column was edited.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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\u2019s all right; we understand. Maybe next time. See you all tomorrow.<\/P>\n<TABLE id=\"EIAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Believe it or not, that last paragraph was <I>not<\/I> an attempt to poke fun at senior citizens; the truth is, we really <I>did<\/I> want to know if JC had time for a long, boring, and completely-pointless story. After all, aren\u2019t long, boring, and completely-pointless stories what <I>Hey, Scripting Guy!<\/I> is all about?<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Given the birthday for one of my Office Outlook contacts, how can I tell how old that person is?&#8212; JC 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,5],"class_list":["post-63693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Given the birthday for one of my Office Outlook contacts, how can I tell how old that person is?&#8212; JC 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=63693"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=63693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}