{"id":55863,"date":"2008-04-03T00:35:00","date_gmt":"2008-04-03T00:35:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/03\/hey-scripting-guy-how-can-i-sort-items-retrieved-from-a-microsoft-outlook-folder\/"},"modified":"2008-04-03T00:35:00","modified_gmt":"2008-04-03T00:35:00","slug":"hey-scripting-guy-how-can-i-sort-items-retrieved-from-a-microsoft-outlook-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-sort-items-retrieved-from-a-microsoft-outlook-folder\/","title":{"rendered":"Hey, Scripting Guy! How Can I Sort Items Retrieved From a Microsoft Outlook Folder?"},"content":{"rendered":"<p><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hey, Scripting Guy! I\u2019m trying to write a script that does something with all the email messages in my Outlook Inbox. However, I need to sort those messages by the date they were received, and I can\u2019t figure out how to do that. Can you help?<br \/>&#8212; SL<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, SL. You know, no doubt a lot of you who read this column have thought, \u201cThe Scripting Guy who writes that column: I bet he\u2019s a Red. He just <i>seems<\/i> like a Red to me.\u201d Well, we have good news for you: you were absolutely right, the Scripting Guy who writes this column <i>is<\/i> a Red. Or, to be a little more precise, red and yellow are his dominant energies, with green and blue his inclined energies.<\/p>\n<p>And yes, now that you see it in print it all seems so obvious, doesn\u2019t it?<\/p>\n<p>So what does all this dominant and inclined energy stuff mean? Well, to be honest, the Scripting Guy who writes this column has no idea. Recently the Scripting Guys\u2019 larger team took part in an Insights Discovery session put on by <a href=\"http:\/\/www.insights.com\/\" target=\"_blank\"><b>Insights<\/b><\/a>. Unfortunately, the Scripting Guy who writes this column had an important meeting with TechNet that same day and was thus unable to attend the session. Because he didn\u2019t make the session he knows only that he\u2019s a Fiery Red (as opposed to a Cool Blue, Earth Green, or Sunshine Yellow); for better or worse, he has no idea what that actually <i>means<\/i>.<\/p>\n<table class=\"dataTable\" id=\"END\" 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>. What\u2019s that? If he hadn\u2019t had the TechNet meeting would the Scripting Guy who writes this column have attended the session? Um, that\u2019s such a silly question we aren\u2019t even going to dignify it with a response.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In addition to being a Fiery Red, the Scripting Guy who writes this column is a Motivator (Orange) on the 72 Type Wheel System. And before you ask, no, he doesn\u2019t know what that means, either. He <i>does<\/i> know, however, that the 72 Type Wheel system has 72 discrete sections that offer \u201can infinite number of colour combinations to represent the uniqueness of each individual.\u201d Admittedly, the Scripting Guy who writes this column isn\u2019t totally sure how you can take Red, Blue, Yellow, and Green and create an infinite number of color combinations. But, then again, Fiery Reds aren\u2019t really known for their math skills. We tend to be people persons, if you know what we mean.<\/p>\n<p>Speaking of people, it turns out that Scripting Guy Jean Ross is a Blue, with blue and red representing her dominant energies and green and yellow representing her inclined energies.<\/p>\n<p>Oh, come on, don\u2019t laugh: she can\u2019t help being who she is. <\/p>\n<p>Meanwhile, Scripting Guy Dean Tsaltas is a Burnt Umber, which is noteworthy for the fact that Burnt Umber isn\u2019t even one of the colors that you <i>can<\/i> be. \u201cWe don\u2019t understand it, either,\u201d said Ken Myer, spokesperson for Insights. \u201cBut every time we ran his profile it came out Burnt Umber.\u201d<\/p>\n<p>Go figure.<\/p>\n<p>At any rate, according to the brochure a Fiery Red is \u201cCompetitive, demanding, determined, strong-willed, and purposeful.\u201d As you can see, these are all outstanding personality traits, and the Scripting Guy who writes this column is glad to see scientific acknowledgment of the fact that he\u2019s just one heck of a guy. By contrast, a Cool Blue, like Scripting Guy Jean Ross, is \u201cdull and boring, and should always defer to a Fiery Red when it comes to work-related issues.\u201d<\/p>\n<p>Or at least we assume that\u2019s what a Cool Blue is. We didn\u2019t really have time to read the entire brochure.<\/p>\n<p>Incidentally, Fiery Reds are also known for their ability to write scripts that can sort the items found in a Microsoft Outlook folder. You know, scripts like the following, which returns a collection of items found in the Inbox, sorted (in descending order) by the date that the item was received: <\/p>\n<pre class=\"codeSample\">On Error Resume Next\n\nConst olFolderInbox = 6\n\nSet objOutlook = CreateObject(\"Outlook.Application\")\n\nSet objNamespace = objOutlook.GetNamespace(\"MAPI\")\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n\nSet colItems = objFolder.Items\ncolItems.Sort \"ReceivedTime\", True\n\nFor Each objItem in colItems\n    Wscript.Echo objItem.ReceivedTime\nNext\n<\/pre>\n<p>So do we have any idea how this script works? Tut-tut; did you forget that the Scripting Guy who writes this column is a Fiery Red? To begin with, we define a constant named olFolderInbox and set the value of that constant to 6; we\u2019ll use olFolderInbox to tell the script which Outlook folder we want to work with. After defining the constant we create an instance of the <b>Outlook.Application<\/b> object, then use these two lines of code to bind to the MAPI namespace (the only namespace we <i>can<\/i> bind to) and then to the Inbox folder:<\/p>\n<pre class=\"codeSample\">Set objNamespace = objOutlook.GetNamespace(\"MAPI\")\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/pre>\n<p>As soon as we\u2019ve made a connection to the Inbox we can go ahead and retrieve a collection of all the mail messages found there; that can be done simply by creating an object reference to the folder\u2019s <b>Items<\/b> collection:<\/p>\n<pre class=\"codeSample\">Set colItems = objFolder.Items\n<\/pre>\n<p>And now comes the really cool part:<\/p>\n<pre class=\"codeSample\">colItems.Sort \"ReceivedTime\", True\n<\/pre>\n<p>Uh, actually, that <i>was<\/i> the really cool part, the part where we sort the returned collection \u2013 in descending order \u2013 by the date that the message was received. To do that we simply call the <b>Sort<\/b> method followed by two parameters: 1) <b>ReceivedTime<\/b>, the name of the property we want to sort by; and, 2) <b>True<\/b>, which tells the script to sort the collection in descending order. What if we wanted to sort the collection in <i>ascending<\/i> order? No problem; just leave off the second parameter:<\/p>\n<pre class=\"codeSample\">colItems.Sort \"ReceivedTime\"\n<\/pre>\n<p>And what if we wanted to sort the collection, in descending order, by, say, the Subject line? That\u2019s fine; just sort by the <b>Subject<\/b> property, and include that optional second parameter:<\/p>\n<pre class=\"codeSample\">colItems.Sort \"Subject\", True\n<\/pre>\n<p>Etc., etc.<\/p>\n<table class=\"dataTable\" id=\"ETF\" 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>. What other property values can you sort by? You must be a Sunshine Yellow, aren\u2019t you? But that\u2019s fine; we have no problem with Sunshine Yellows. (Earth Greens, of course, are a different story.) Well, Sunshine Yellow, with <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa210304(office.11).aspx\" target=\"_blank\"><b>a few exceptions<\/b><\/a> you can sort your collection by any of the properties of Outlook\u2019s <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa210946(office.11).aspx\" target=\"_blank\"><b>MailItem object<\/b><\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>All we have to do now is see if this actually works. To do that, we set up a For Each loop designed to walk us through each item in the Inbox collection:<\/p>\n<pre class=\"codeSample\">For Each objItem in colItems\n<\/pre>\n<p>And what can we do inside this loop? Pretty much anything we want. For today\u2019s sample script, all we do is echo back the value of each item\u2019s ReceivedTime property:<\/p>\n<pre class=\"codeSample\">Wscript.Echo objItem.ReceivedTime\n<\/pre>\n<p>That results in output similar to this:<\/p>\n<pre class=\"codeSample\">4\/1\/2008 8:11:23 AM\n4\/1\/2008 8:00:12 AM\n4\/1\/2008 7:59:16 AM\n4\/1\/2008 7:44:47 AM\n4\/1\/2008 7:43:26 AM\n4\/1\/2008 7:33:39 AM\n3\/31\/2008 4:17:42 PM\n3\/31\/2008 12:55:12 PM\n<\/pre>\n<p>Pretty cool, huh? And, again, to sort in ascending order just leave off the second parameter.<\/p>\n<p>So, in retrospect, does the Scripting Guy who writes this column wish that he had been able to make it to the Insights Discovery session? To be honest, no; for him it would have been a waste of time. After all, Insights Discovery is designed to help you become a better teammate, to help you better communicate with your co-workers, to help you learn to value and respect other people and their opinions. Needless to say, the Scripting Guy who writes this column has already mastered all of these things; no one is more sensitive to the needs and feelings if his co-workers than he is.<\/p>\n<p>And if you don\u2019t believe that, just ask the Scripting Editor; she\u2019ll tell you what a kind-hearted and sensitive soul he truly is. Although you might have to speak slowly and avoid using big words; after all, she <i>is<\/i> a Cool Blue, you know.<\/p>\n<p>Just like we said: sensitive to the unique needs of his co-workers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m trying to write a script that does something with all the email messages in my Outlook Inbox. However, I need to sort those messages by the date they were received, and I can\u2019t figure out how to do that. Can you help?&#8212; SL Hey, SL. You know, no doubt a lot [&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-55863","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! I\u2019m trying to write a script that does something with all the email messages in my Outlook Inbox. However, I need to sort those messages by the date they were received, and I can\u2019t figure out how to do that. Can you help?&#8212; SL Hey, SL. You know, no doubt a lot [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55863","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=55863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55863\/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=55863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}