How Can I Randomly Select an Email From an Outlook Mail Folder?

How Can I Randomly Select an Email From an Outlook Mail Folder?

  • Comments 1
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I randomly select an email from an Outlook mail folder?

-- TV

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TV. You know, for a brief moment last night the Scripting Guy who writes this column thought he had achieved every father’s dream. The Scripting Dad and the Scripting Son went to the gym to play basketball, and when they were done the Scripting Son said, “Hey, Dad. Can I ask you something? It’s for my school project.”

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 – like so many other people throughout the world – had no doubts about who his hero was: the Scripting Guy who writes this column.

“Sure,” said the Scripting Dad, trying to suppress the giant smile that was building up inside him. “Ask me anything you want.”

“OK,” said the Scripting Son. “What’s your favorite Dr. Seuss book?”

“What? Dr. Seuss book? I don’t know. Green Eggs and Ham?”

“OK, thanks.”

And that was it; as it turns out, the Scripting Son was working on a second school project, one involving statistics and opinion polls. He needed the Scripting Dad’s help simply because he had to ask 100 people what their favorite Dr. Seuss book was.

For his other school project, the one where he had to write about his hero, the Scripting Son chose Vladimir Guerrero 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’t so!

Note. Yes, the Scripting Guy who writes this column knows that the team is no longer called the California Angels; they’re now the Los Angeles Angels of Anaheim. On the other hand, the team changes its name about as often as – well, the team changes its name an awful lot, so it’s just a matter of time before they’re the California Angels again.

Now that you mention it, the Scripting Guy who writes this column was going to say that the team changes its name “as often as a woman changes her hairstyle.” 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, “as often as a woman changes her mind” would have been good, too.

Before you ask, no, changing that to “as often as a man changes his mind” wouldn’t work. After all, the Scripting Guy who writes this column has never known a man to change his mind, largely because men never let silly things like facts get in their way.

To make a long story short, it turns out that the Scripting Dad isn’t the Scripting Son’s hero. (Although the Scripting Dad doesn’t remember Vladimir Guerrero helping pay for the Scripting Son’s summer baseball team.) But maybe he can be your hero, TV. He’s probably not going to help pay for your summer baseball team (sorry), but he will give you a script that lets you randomly select an email from an Outlook mail folder:

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)

strFolderName = objInbox.Parent

Set objMailbox = objNamespace.Folders(strFolderName)
Set objFolder = objMailbox.Folders("2007 Scripting Games")

Set colItems = objFolder.Items
intHighNumber = colItems.Count

intLowNumber = 1

Randomize
intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    
Wscript.Echo colItems(intNumber).SenderName

OK, so maybe that doesn’t look very heroic. But that’s OK; you’ll 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’s 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 Outlook.Application object:

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)

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’t subfolders of the Inbox they’re subfolders of the Mailbox folder. For example, suppose we want to use the following Outlook setup and work with the 2007 Scripting Games folder:

Microsoft Outlook


As you can see, 2007 Scripting Games is a subfolder of Mailbox, not Inbox. So then why did we connect to the Inbox folder?

Don’t ask us; why don’t you go ask Vladimir Guerrero?

No, wait; Vlad’s 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’t connect to the Mailbox folder without specifying the folder name. That’s a bit of a problem, because the folder isn’t actually named Mailbox; in this case, for example, it’s named Mailbox – 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 their Mailbox folder. That didn’t 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’s Parent folder:

strFolderName = objInbox.Parent

As you might expect, the Parent folder for the Inbox just happens to be the Mailbox folder.

Once the name of the Mailbox folder is safely stashed in the variable strFolderName we then execute these two lines of code:

Set objMailbox = objNamespace.Folders(strFolderName)
Set objFolder = objMailbox.Folders("2007 Scripting Games")

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’re ready for action.

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’t 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’s Count property in a variable named intHighNumber:

Set colItems = objFolder.Items
intHighNumber = colItems.Count

Again, let’s assume there are 77 items in the folder. In that case, we’re going to randomly select a number between something and 77. What’s something? Well, most likely that’s 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:

intLowNumber = 1

With our numeric range established we next call the Randomize function, an act that “seeds” 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):

intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

Note. We won’t bother explaining this line of code today. If you’d like a little more information about generating random numbers, well, we just happen to have a Hey, Scripting Guy! column on that very subject.

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 SenderName property for item 13 in the collection:

Wscript.Echo colItems(intNumber).SenderName

Note that, because we’re dealing with SenderName, Outlook is going to pop up a security dialog box and we’re 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 Office Space article on, well, that very subject.

Well, not entirely on that subject. But we do talk about it.

By the way, we should also note that, after all this, the Scripting Dad isn’t even sure that Green Eggs and Ham even is his favorite Dr. Seuss book. After all, there are the two Horton books (Horton Hears a Who and Horton Hatches the Egg). And then there’s the vastly-underrated Yertle the Turtle. (Which, as Lisa Simpson once noted, remains the best book ever written on the subject of turtle stacking.) In fact, as a father, there’s really only one Dr. Seuss book that the Scripting Dad finds a bit unsettling: Hop on Pop. Never, ever let your children read that one.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment