{"id":64133,"date":"2007-08-29T01:44:00","date_gmt":"2007-08-29T01:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/29\/how-can-i-get-a-list-of-appointments-for-a-specific-month\/"},"modified":"2007-08-29T01:44:00","modified_gmt":"2007-08-29T01:44:00","slug":"how-can-i-get-a-list-of-appointments-for-a-specific-month","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-list-of-appointments-for-a-specific-month\/","title":{"rendered":"How Can I Get a List of Appointments for a Specific Month?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I get a list of my appointments for a specific month (for example, all my appointments for December, 2007)?<BR><BR>&#8212; HG <\/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, HG. We\u2019ll get to your question in just a moment. Before we do that, however, the Scripting Guy who writes this column wants to take a little time and enjoy the last few days of August (and, in effect, the last few days of summer). And what a summer it\u2019s been here in the Seattle area; this past weekend, for example, the temperature nearly reached 70 degrees Fahrenheit! (And no, we are <I>not<\/I> making that up.) At one point the Scripting Guy who writes this column actually had the back door to his house propped open, although that changed the moment the wind started blowing and the rain started falling. But that was all right: needless to say, after dealing with daytime temperatures of 68 degrees Fahrenheit, well, a little breeze and a little rain felt pretty good.<\/P>\n<P>The important thing is that, after a relatively tough winter (which really wasn\u2019t all <I>that<\/I> tough, as long you didn\u2019t mind <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jan07\/hey0104.mspx\"><B>being without power<\/B><\/A> for a 6-day stretch) the weather gods obviously felt sorry for those of us who live in Seattle. \u201cThose poor Seattle-area people,\u201d they must have thought. \u201cWe gave them such a lousy winter; how can we make it up to them? Oh, wait, we know: we\u2019ll give them a summer where the temperature rarely climbs above 70 degrees and the sun hardly ever shines. That should make everyone happy.\u201d <\/P>\n<P>And, of course, it did.<\/P>\n<TABLE class=\"dataTable\" id=\"ELD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. The whole Seattle area seems to be under a curse of some kind: we\u2019re spending billions for dollars to build a light rail system that doesn\u2019t actually go anywhere; we\u2019re stuck serving as the home of the Seattle Supersonics (although that, at least, appears to be temporary); and now we\u2019re forced to endure both a lousy winter <I>and<\/I> a lousy summer. What did we ever do to deserve this?<\/P>\n<P>Hey, wait a minute: who said, \u201cYou gave the world Microsoft, didn\u2019t you?\u201d<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Oh, well. As it turns out, that tiny glimmer of light that might have been a tiny of sliver of sunshine has now disappeared behind our omnipresent gray clouds. With that in mind, let\u2019s turn the lights on, crank up the heat a little, and conclude the summer with a bang: by writing a script that retrieves a list of appointments scheduled for the month of December, 2007.<\/P>\n<P>You know, a script like this one:<\/P><PRE class=\"codeSample\">Const olFolderCalendar = 9<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)<\/p>\n<p>Set colItems = objFolder.Items<\/p>\n<p>strFilter = &#8220;[Start] &gt;= &#8217;12\/1\/2007&#8242; AND [Start] &lt;= &#8217;12\/31\/2007&#8242;&#8221;\nSet colFilteredItems = colItems.Restrict(strFilter)<\/p>\n<p>For Each objItem In colFilteredItems\n    If objItem.Start &gt; Now Then\n        Wscript.Echo &#8220;Meeting name: &#8221; &amp; objItem.Subject\n        Wscript.Echo &#8220;Meeting date: &#8221; &amp; objItem.Start\n        Wscript.Echo &#8220;Duration: &#8221; &amp; objItem.Duration &amp; &#8221; minutes&#8221;\n        Wscript.Echo &#8220;Location: &#8221; &amp; objItem.Location\n        Wscript.Echo\n    End If\nNext\n<\/PRE>\n<P>As you can see, we start out by defining a constant named olFolderCalendar and setting the value to 9; we\u2019ll use that constant to tell the script which Outlook folder we want to connect to. (As a matter of fact, we <I>do<\/I> want to connect to the Calendar folder. Good guess!) After defining the constant, we create an instance of the <B>Outlook.Application<\/B> object, then use the <B>GetNamespace<\/B> method to bind to the MAPI namespace (which \u2013 in case you\u2019re wondering \u2013 happens to be the <I>only<\/I> namespace that we can bind to).<\/P>\n<TABLE class=\"dataTable\" id=\"ELE\" 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>. So if the MAPI namespace is the only namespace we can bind to, then do we even <I>have<\/I> to bind to it? Why don\u2019t we just automatically connect to the MAPI namespace? Well, that\u2019s because \u2026 because \u2026 well, just because, OK?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we\u2019ve made the connection to the MAPI namespace, we then call the <B>GetDefaultFolder<\/B> method to hook us up to the Outlook calendar (note that we pass GetDefaultFolder the constant olFolderCalendar that we created at the start of the script):<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)\n<\/PRE>\n<P>From there, we can get a collection of all our appointments by doing nothing more complicated than creating an object reference to the folder\u2019s <B>Items<\/B> property, like so:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>That was pretty easy, wasn\u2019t it? But party\u2019s over: now we\u2019re going to have to do a little bit of work. (But don\u2019t worry; it truly <I>is<\/I> just a \u201clittle bit\u201d of work.) As you no doubt recall, we aren\u2019t interested in a collection of <I>all<\/I> our appointments; we\u2019re only interested in those appointments scheduled to take place during the month of December, 2007. Therefore, and in order to restrict our collection to appointments scheduled for the month of December, 2007, we need to apply a <B>Filter<\/B> to the collection, something we do with these two lines of code:<\/P><PRE class=\"codeSample\">strFilter = &#8220;[Start] &gt;= &#8217;12\/1\/2007&#8242; AND [Start] &lt;= &#8217;12\/31\/2007&#8242;&#8221;\nSet colFilteredItems = colItems.Restrict(strFilter)\n<\/PRE>\n<P>So what\u2019s going on here? Well, in the first line we\u2019re setting the criteria for the Filter: we only want those appointments where the value of the <B>Start<\/B> property (note that property names must be enclosed in square brackets) is greater than or equal to December 1, 2007 (12\/1\/2007) <I>and<\/I> where the value of the Start property is less than or equal to December 31, 2007. The net effect? We\u2019ll only get back appointments that take place (i.e., that start) in December, 2007.<\/P>\n<P>In the second line of code, we simply call the <B>Restrict<\/B> method, passing along the filter we created (represented by the variable strFilter). That\u2019s going to give us back a brand-new collection, one consisting only of those appointments that take place in December, 2007. From there all we have to do is set up a For Each loop to loop through this sub-collection, doing nothing more inside that loop than echoing back some of the key appointment property values (such as meeting name, date, duration, and location):<\/P><PRE class=\"codeSample\">For Each objItem In colFilteredItems\n    If objItem.Start &gt; Now Then\n        Wscript.Echo &#8220;Meeting name: &#8221; &amp; objItem.Subject\n        Wscript.Echo &#8220;Meeting date: &#8221; &amp; objItem.Start\n        Wscript.Echo &#8220;Duration: &#8221; &amp; objItem.Duration &amp; &#8221; minutes&#8221;\n        Wscript.Echo &#8220;Location: &#8221; &amp; objItem.Location\n        Wscript.Echo\n    End If\nNext\n<\/PRE>\n<P>That\u2019s all there is to it.<\/P>\n<P>Now, one thing to keep in mind here is that this script will not necessarily show any recurring appointments that take place in December, 2007; that\u2019s because Outlook treats recurring appointments slightly different than it does other meetings. If the first occurrence of a recurring appointment takes place in December 2007 then that first occurrence will be returned by the script. However, if the first occurrence for the recurring appointment took place earlier then December, 2007, well \u2026. For HG this wasn\u2019t a problem; she was specifically interested in \u201cone-off\u201d meetings and appointments. However, if that <I>is<\/I> a problem for you (that is, if you\u2019d like to get a list of recurring appointments as well), take a look at the Office Space article <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/sept05\/tips0901.mspx\"><B>Retrieving a List of Recurring Appointments from Microsoft Outlook<\/B><\/A> and see if that helps.<\/P>\n<TABLE class=\"dataTable\" id=\"ELG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Office Outlook 2007:<\/B> If you run this script against Outlook 2007, you will see <I>some<\/I> recurring meetings that fall in December 2007. However, you\u2019ll see only those recurring meetings where the first occurrence hasn\u2019t happened yet. In addition, the meeting date (objItem.Start) will show the first date in the recurring appointment. For example, if you have a recurring meeting the first Friday of every month starting in October 2007, the meeting will show up in your output for December meetings (since there will be a meeting scheduled for Friday, December 7) but the meeting date of your output will show October 5. If the first occurrence of that meeting was in the past (say, June 1, 2007) you won\u2019t see the December occurrence.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And yes, we know: some of you are interested in retrieving a list of the actual days and dates for recurring appointments (as opposed to a note that simply says that this meeting occurs every Tuesday from now until January 1, 2008). That\u2019s something we\u2019ll probably address in a future <I>Hey, Scripting Guy!<\/I> But not today; today isn\u2019t the future. <\/P>\n<P>Or at least we <I>hope<\/I> that today isn\u2019t the future. Things are depressing enough around here as they are. Oh, well; tomorrow the temperature is expected to reach <I>74<\/I> degrees Fahrenheit. Talk about a heat wave, eh?<\/P>\n<TABLE class=\"dataTable\" id=\"EGH\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Ed<\/B><B>itor\u2019s Note:<\/B> The Scripting Guy who writes this column went to the dentist today. We hope that explains any small hint of grouchiness you might have detected in today\u2019s column. We\u2019re not sure how to explain yesterday\u2019s column\u2026or the day before\u2026or\u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of my appointments for a specific month (for example, all my appointments for December, 2007)?&#8212; HG Hey, HG. We\u2019ll get to your question in just a moment. Before we do that, however, the Scripting Guy who writes this column wants to take a little time and [&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-64133","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 get a list of my appointments for a specific month (for example, all my appointments for December, 2007)?&#8212; HG Hey, HG. We\u2019ll get to your question in just a moment. Before we do that, however, the Scripting Guy who writes this column wants to take a little time and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64133","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=64133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64133\/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=64133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}