{"id":64643,"date":"2007-06-16T00:00:00","date_gmt":"2007-06-16T00:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/16\/how-can-i-randomly-select-an-email-from-an-outlook-mail-folder\/"},"modified":"2007-06-16T00:00:00","modified_gmt":"2007-06-16T00:00:00","slug":"how-can-i-randomly-select-an-email-from-an-outlook-mail-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-randomly-select-an-email-from-an-outlook-mail-folder\/","title":{"rendered":"How Can I Randomly Select an Email From an Outlook Mail Folder?"},"content":{"rendered":"<p><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\"> <\/P>\n<P>Hey, Scripting Guy! How can I randomly select an email from an Outlook mail folder?<BR><BR>&#8212; TV <\/P>\n<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> <\/P>\n<P>Hey, TV. You know, for a brief moment last night the Scripting Guy who writes this column thought he had achieved every father\u2019s dream. The Scripting Dad and the Scripting Son went to the gym to play basketball, and when they were done the Scripting Son said, \u201cHey, Dad. Can I ask you something? It\u2019s for my school project.\u201d<\/P>\n<P>Now, to all outward appearances, the Scripting Guy who writes this column remained as cool and implacable as he always is. Inside, however, he was very excited. After all, the Scripting Son had recently been given an assignment to write about his hero. Obviously the Scripting Son \u2013 like so many other people throughout the world \u2013 had no doubts about who <I>his<\/I> hero was: the Scripting Guy who writes this column.<\/P>\n<P>\u201cSure,\u201d said the Scripting Dad, trying to suppress the giant smile that was building up inside him. \u201cAsk me anything you want.\u201d<\/P>\n<P>\u201cOK,\u201d said the Scripting Son. \u201cWhat\u2019s your favorite Dr. Seuss book?\u201d <\/P>\n<P>\u201cWhat? Dr. Seuss book? I don\u2019t know. <I>Green Eggs and Ham<\/I>?\u201d<\/P>\n<P>\u201cOK, thanks.\u201d<\/P>\n<P>And that was it; as it turns out, the Scripting Son was working on a <I>second<\/I> school project, one involving statistics and opinion polls. He needed the Scripting Dad\u2019s help simply because he had to ask 100 people what their favorite Dr. Seuss book was.<\/P>\n<P>For his <I>other<\/I> school project, the one where he had to write about his hero, the Scripting Son chose <A href=\"http:\/\/losangeles.angels.mlb.com\/team\/player.jsp?player_id=115223\" target=\"_blank\"><B>Vladimir Guerrero<\/B><\/A> of the California Angels. In other words, he chose a baseball player (a good baseball player, but a baseball player nevertheless) as his hero. A baseball player rather than a Scripting Guy?!? Say it isn\u2019t so!<\/P>\n<P>\n<TABLE class=\"dataTable\" id=\"EYD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Yes, the Scripting Guy who writes this column knows that the team is no longer called the California Angels; they\u2019re now the Los Angeles Angels of Anaheim. On the other hand, the team changes its name about as often as \u2013 well, the team changes its name an awful lot, so it\u2019s just a matter of time before they\u2019re the California Angels again.<\/P>\n<P>Now that you mention it, the Scripting Guy who writes this column <I>was<\/I> going to say that the team changes its name \u201cas often as a woman changes her hairstyle.\u201d However, seeing as how the Scripting Editor is a woman (or at least purports to be) he knew that saying something like that would be a major mistake. Although now that he thinks about it, \u201cas often as a woman changes her mind\u201d would have been good, too.<\/P>\n<P>Before you ask, no, changing that to \u201cas often as a man changes <I>his<\/I> mind\u201d wouldn\u2019t work. After all, the Scripting Guy who writes this column has never known a man to <I>change<\/I> his mind, largely because men never let silly things like facts get in their way.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/P>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>To make a long story short, it turns out that the Scripting Dad <I>isn\u2019t<\/I> the Scripting Son\u2019s hero. (Although the Scripting Dad doesn\u2019t remember Vladimir Guerrero helping pay for the Scripting Son\u2019s summer baseball team.) But maybe he can be <I>your<\/I> hero, TV. He\u2019s probably not going to help pay for <I>your<\/I> summer baseball team (sorry), but he <I>will<\/I> give you a script that lets you randomly select an email from an Outlook mail folder:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objInbox = objNamespace.GetDefaultFolder(olFolderInbox)<\/p>\n<p>strFolderName = objInbox.Parent<\/p>\n<p>Set objMailbox = objNamespace.Folders(strFolderName)\nSet objFolder = objMailbox.Folders(&#8220;2007 Scripting Games&#8221;)<\/p>\n<p>Set colItems = objFolder.Items\nintHighNumber = colItems.Count<\/p>\n<p>intLowNumber = 1<\/p>\n<p>Randomize\nintNumber = Int((intHighNumber &#8211; intLowNumber + 1) * Rnd + intLowNumber)<\/p>\n<p>Wscript.Echo colItems(intNumber).SenderName\n<\/PRE>\n<P>OK, so maybe that doesn\u2019t <I>look<\/I> very heroic. But that\u2019s OK; you\u2019ll probably change your mind as soon as we explain how the script works. To begin with, we define a constant named olFolderInbox and set the value to 6; that\u2019s going to tell the script which Outlook folder we want to connect to. (More on that in a second.) We then use this block of code to create an instance of the <B>Outlook.Application<\/B> object:<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objInbox = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>Some of you might be wondering why we connected to the Inbox rather than to the Mailbox folder; after all, in many cases Outlook mail folders aren\u2019t subfolders of the Inbox they\u2019re subfolders of the Mailbox folder. For example, suppose we want to use the following Outlook setup and work with the 2007 Scripting Games folder:<\/P>\n<P><IMG height=\"314\" alt=\"Microsoft Outlook\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/outlookfolders.jpg\" width=\"172\" border=\"0\"> <\/P>\n<P><BR>As you can see, 2007 Scripting Games is a subfolder of Mailbox, not Inbox. So then why did we connect to the Inbox folder?<\/P>\n<P>Don\u2019t ask us; why don\u2019t you go ask Vladimir Guerrero?<\/P>\n<P>No, wait; Vlad\u2019s not going to help you, not unless you have the bases loaded and need somebody to whack one off the centerfield fence. We connected to the Inbox simply because we can\u2019t connect to the Mailbox folder without specifying the folder name. That\u2019s a bit of a problem, because the folder isn\u2019t actually named Mailbox; in this case, for example, it\u2019s named Mailbox \u2013 Greg Stemp. We could have hard-coded that name into the script, but then every single person who tried to run this piece of code would need to go in and change that value to reflect the name of <I>their<\/I> Mailbox folder. That didn\u2019t seem like much fun, so we opted to connect to the Inbox, then use this line of code to retrieve the name of the Inbox\u2019s <B>Parent<\/B> folder:<\/P><PRE class=\"codeSample\">strFolderName = objInbox.Parent\n<\/PRE>\n<P>As you might expect, the Parent folder for the Inbox just happens to be the Mailbox folder.<\/P>\n<P>Once the name of the Mailbox folder is safely stashed in the variable strFolderName we then execute these two lines of code:<\/P><PRE class=\"codeSample\">Set objMailbox = objNamespace.Folders(strFolderName)\nSet objFolder = objMailbox.Folders(&#8220;2007 Scripting Games&#8221;)\n<\/PRE>\n<P>In line 1 we make a connection to the Mailbox folder; in line 2 we make a connection (with the object reference objFolder) to our target folder: 2007 Scripting Games. And now we\u2019re ready for action.<\/P>\n<P>In order to randomly select an email we need to pick a number between 1 and the number of items in the folder. (That should make sense; if we have 77 items in the folder we can\u2019t randomly choose item number 1918.) To determine the number of items in the 2007 Scripting Games folder we use these two lines of code to grab a collection of all those items and then store the value of the collection\u2019s <B>Count<\/B> property in a variable named intHighNumber:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\nintHighNumber = colItems.Count\n<\/PRE>\n<P>Again, let\u2019s assume there are 77 items in the folder. In that case, we\u2019re going to randomly select a number between something and 77. What\u2019s something? Well, most likely that\u2019s going to be 1 (thus making our choice a number between 1 and 77). With that in mind we assign the value 1 to a variable named intLowNumber:<\/P><PRE class=\"codeSample\">intLowNumber = 1\n<\/PRE>\n<P>With our numeric range established we next call the <B>Randomize<\/B> function, an act that \u201cseeds\u201d our random number generator. After that we use this crazy-looking line of code to generate a random integer value between 1 and 77 (or however many items we have in our folder):<\/P><PRE class=\"codeSample\">intNumber = Int((intHighNumber &#8211; intLowNumber + 1) * Rnd + intLowNumber)\n<\/PRE>\n<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>Note<\/B>. We won\u2019t bother explaining this line of code today. If you\u2019d like a little more information about generating random numbers, well, we just happen to have a <I>Hey, Scripting Guy!<\/I> column on <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may05\/hey0518.mspx\"><B>that very subject<\/B>.<\/A><\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/P>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what do we do with this random number now that we have it? Well, we can use it as an index number for our collection of all the items found in the 2007 Scripting Games folder (a collection named colItems). Suppose our random number is 13. That means that this line of code will echo back the value of the <B>SenderName<\/B> property for item 13 in the collection:<\/P><PRE class=\"codeSample\">Wscript.Echo colItems(intNumber).SenderName\n<\/PRE>\n<P>Note that, because we\u2019re dealing with SenderName, Outlook is going to pop up a security dialog box and we\u2019re going to have to explicitly give the script access to this property. Some properties (like SenderName and anything representing an email address) require you to go through this security procedure. Other properties (such as Subject) can be retrieved automatically. For more information, see our <I>Office Space<\/I> article on, well, <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/may05\/tips0524.mspx\"><B>that very subject<\/B><\/A>.<\/P>\n<P>Well, not <I>entirely<\/I> on that subject. But we do talk about it.<\/P>\n<P>By the way, we should also note that, after all this, the Scripting Dad isn\u2019t even sure that <I>Green Eggs and Ham<\/I> even <I>is<\/I> his favorite <A href=\"http:\/\/www.seussville.com\/\" target=\"_blank\"><B>Dr. Seuss book<\/B><\/A>. After all, there <I>are<\/I> the two Horton books (<I>Horton Hears a Who<\/I> and <I>Horton Hatches the Egg<\/I>). And then there\u2019s the vastly-underrated <I>Yertle the Turtle<\/I>. (Which, as Lisa Simpson once noted, remains the best book ever written on the subject of turtle stacking.) In fact, as a father, there\u2019s really only one Dr. Seuss book that the Scripting Dad finds a bit unsettling: <I>Hop on Pop<\/I>. Never, ever let your children read <I>that<\/I> one.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I randomly select an email from an Outlook mail folder?&#8212; TV Hey, TV. You know, for a brief moment last night the Scripting Guy who writes this column thought he had achieved every father\u2019s dream. The Scripting Dad and the Scripting Son went to the gym to play basketball, 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-64643","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 randomly select an email from an Outlook mail folder?&#8212; TV Hey, TV. You know, for a brief moment last night the Scripting Guy who writes this column thought he had achieved every father\u2019s dream. The Scripting Dad and the Scripting Son went to the gym to play basketball, and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64643","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=64643"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64643\/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=64643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}