{"id":65363,"date":"2007-03-08T01:12:00","date_gmt":"2007-03-08T01:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/08\/how-can-i-tell-if-any-of-my-contacts-have-a-birthday-this-month\/"},"modified":"2007-03-08T01:12:00","modified_gmt":"2007-03-08T01:12:00","slug":"how-can-i-tell-if-any-of-my-contacts-have-a-birthday-this-month","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-any-of-my-contacts-have-a-birthday-this-month\/","title":{"rendered":"How Can I Tell If Any of My Contacts Have a Birthday This Month?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I tell if any of my contacts have a birthday this month?<BR><BR>&#8212; VG<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>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\u2019t 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.<\/P>\n<P>But, if you insist, send your gifts here:<\/P>\n<P>The Scripting Guy Who Writes That Column<BR>Microsoft Corporation<BR>Building 42\/4039<BR>One Microsoft Way<BR>Redmond, WA 98052<\/P>\n<P>And yes, cash will be fine. Thanks!<\/P>\n<P><I>Editor\u2019s Note: Just kidding everyone. It\u2019s not even his birthday (read on). He tries to get free desserts <\/I><I>from restaurants <\/I><I>like this too.<\/I><\/P>\n<P>Of course, now we feel bad that we didn\u2019t get you anything for <I>your<\/I> 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 \u2018R Us. We\u2019ll be right back.<\/P>\n<P>Whew; we got the last one they had in stock. Hope this fits, VG:<\/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 Month(objContact.Birthday) = Month(Date) Then\n        Wscript.Echo objContact.FullName, objContact.Birthday\n    End If\nNext\n<\/PRE>\n<P>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\u2019s 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\u2019ll 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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0616.mspx\"><B>Hey, Scripting Guy! archive<\/B><\/A>.<\/P>\n<P>As for the script itself, we start things off with the On Error Resume Next statement. That\u2019s something we typically don\u2019t use in our scripts, but we occasionally encountered an error when dealing with contact birthdays. We\u2019re not totally sure <I>why<\/I> 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\u2019ll let you know.<\/P>\n<P>But don\u2019t hold your breath waiting.<\/P>\n<P>After enabling error handling we define a constant named olFolderContacts; we\u2019ll 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 <B>Outlook.Application<\/B> object and to bind to the MAPI namespace:<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\n<\/PRE>\n<P>We should also point out that this script assumes that Outlook is already up and running. What are you supposed to do if Outlook <I>isn\u2019t<\/I> 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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/jun05\/tips0614.mspx\"><B>Office Space archive<\/B><\/A>.<\/P>\n<P>Now, in theory, we could probably write a complicated filter that would automatically weed out contacts that don\u2019t 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\u2019s birthday. Therefore, our next step is to use this line of code to retrieve a collection of all the contacts in the Contacts folder:<\/P><PRE class=\"codeSample\">Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items\n<\/PRE>\n<P>What are we going to do with that collection? Funny you should ask. To begin with, we\u2019re going to set up a For Each loop to walk through the entire collection. For each contact in that collection we\u2019ll use this line of code to determine whether the contact has a birthday in the current month:<\/P><PRE class=\"codeSample\">If Month(objContact.Birthday) = Month(Date) Then\n<\/PRE>\n<P>All we\u2019re doing here is using the command <B>Month(objContact.Birthday) <\/B>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\u2019s what <B>Month(Date)<\/B> tells us. If the values match then the contact has a birthday this month. In turn, we echo back the contact name and birthday:<\/P><PRE class=\"codeSample\">Wscript.Echo objContact.FullName, objContact.Birthday\n<\/PRE>\n<P>If the two months <I>don\u2019t<\/I> match then we simply loop around and check the next contact in the collection.<\/P>\n<P>When we\u2019re all done we should get back a report similar to this (assuming that the script is run sometime in March):<\/P><PRE class=\"codeSample\">Jonathan Haas 3\/28\/1977\nKen Myer 3\/21\/1938\nPilar Ackerman 3\/14\/1968\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EIF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. 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!?!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>One thing to watch out for here. If you haven\u2019t specified a birthday for a contact Outlook automatically assigns that person a birthday of 1\/1\/4501. (Interestingly, that\u2019s the same year that we expect Scripting Guy Jean Ross to finally stop complaining about our good-natured jab about her age. <I>[Editor\u2019s Note: Actually, Jean <\/I><I>thinks it wouldn\u2019t be <\/I>all<I> bad<\/I><I> for people to believe she was born in 1938; she\u2019d get senior discounts<\/I><I>,<\/I><I> and a lot of comments on how great she looks for her age<\/I><I> \u2013 not that she doesn\u2019t get those comments anyway<\/I><I>.]<\/I>) 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:<\/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        If Month(objContact.Birthday) = Month(Date) Then\n            Wscript.Echo objContact.FullName, objContact.Birthday\n        End If\n    End If\nNext\n<\/PRE>\n<P>OK, we need to come clean with you here: the Scripting Guy who writes this column had his birthday on December 18<SUP>th<\/SUP>, 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\u2019t send him a birthday present after all? Well, you know, it\u2019s never too early to get a jump on his <I>next<\/I> birthday \u2026. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if any of my contacts have a birthday this month?&#8212; VG 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\u2019t need to send him a present; after all, he writes this [&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-65363","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! How can I tell if any of my contacts have a birthday this month?&#8212; VG 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\u2019t need to send him a present; after all, he writes this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65363","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=65363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65363\/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=65363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}