How Can I Get Access to a Mail Folder That Isn’t a Subfolder of My Outlook Inbox?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I access a specified mail folder in my Outlook Mailbox, a folder that isn’t a subfolder of my Inbox?

— RT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RT. As you recall, in our last exciting episode – what do you mean you don’t recall any exciting episodes when it comes to this column? Come on: this is Hey, Scripting Guy!, the Kingda Ka of the Scripting World! Remember that episode where we showed people how they could create 150 copies of a single file? You’re telling us that didn’t have you on the edge of your seats?

OK, true. But what about that episode where we – well, come to think of it, that really wasn’t all that exciting, was it? But then there was the time that we – well, no: that was useful, but it wasn’t exactly a thrill-a-minute. Let’s see what we have here: adding leading zeroes to all the lines in a text file; renaming a local user account on a Windows XP computer; counting the number of words in a text file – you know, maybe you’re right. Let’s start over.

Hey, RT. As you recall, in our last less-than-exciting episode we promised to show you how to access a Microsoft Outlook folder that wasn’t a subfolder of your Inbox. What do we mean by that? Well, consider the following Outlook folder structure:

Microsoft Outlook

As you can see, the Mailbox includes a number of email folders, folders like Art Submissions, Blogs, Europe, and Fixes Required. As you can see, none of these folders are subfolders of the Inbox; in fact, in the great Outlook folder structure, they all exist at the exact same level as the Inbox folder. That’s great, except for one thing: how the heck do you access a folder that isn’t a subfolder of the Inbox?

Here’s how:

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(“Europe”)

Set colItems = objFolder.Items

For Each objItem in colItems Wscript.Echo objItem.Subject Next

Granted, if this was an exciting column we’d let you enjoy the thrill and adventure of trying to figure out for yourself how this script works. But seeing as how this isn’t an exciting column we’ll go ahead and explain it all for you.

To begin with, we define a constant named olFolderInbox and set the value to 6. Now, don’t panic: we know that we aren’t interested in the Inbox or any of the subfolders of the Inbox. We’ll explain why we need this constant in just a second.

After defining the constant we create an instance of the Outlook.Application object and use the GetNamespace method to bind to the MAPI namespace (the only namespace we can bind to). We then use this line of code to bind to the Inbox folder:

Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)

No, we didn’t forget: there’s a reason we start out by binding to the Inbox. In this script we’re trying to retrieve information from the Europe folder, which happens to be a subfolder of the Mailbox – Ken Myer folder. In order to get to the Europe folder we need to first bind to Mailbox – Ken Myer; we can bind to the Europe folder only after we’ve done that. We need to work our way through the folder structure, one level at a time.

So then why do we start off by connecting to the Inbox folder? Well, in this case we know the name of the mailbox folder: Mailbox – Ken Myer. That’s great. But suppose you share this script with someone else. In that case, the odds are pretty slim that their mailbox will also be named Mailbox – Ken Myer. What we’re trying to do here is develop a script that can connect to any mailbox folder, without having to hardcode the mailbox name into the code.

Make sense? The only problem with that is the fact that we don’t know of any way to directly retrieve the mailbox name. (We’re not saying that there isn’t such a way; we just don’t know what it is.) However, we do know that the mailbox name can be retrieved by binding to the Inbox and then grabbing the value of the Parent property. Because the Inbox is a child folder of the mailbox, the Inbox Parent will be – tah-dah! – the mailbox. That’s what this line of code is for:

strFolderName = objInbox.Parent

Once we know the name of the mailbox (which we stash inside the variable strFolderName) we can then use this line of code to create an object reference to the mailbox folder:

Set objMailbox = objNamespace.Folders(strFolderName)

Was that worth the effort? You bet it was; after all, once we’re connected to the mailbox we can then bind to any subfolder found inside that mailbox folder. Want to connect to the Europe folder? Then just do this:

Set objFolder = objMailbox.Folders(“Europe”)

See how that works? We simply reference the mailbox Folders collection, specifying the name of the folder (Europe) that we want to connect to. In turn, that gives us an object reference (objFolder) that points to the Europe folder.

From that point on it’s a piece of cake. To retrieve a collection of all the items (email messages) in the Europe folder we use this line of code:

Set colItems = objFolder.Items

And from there we simply set up a For Each loop to loop through all the items in the collection. In this little sample script all we do is echo back the Subject of each message, although once you have this collection you can do pretty much anything you want with it.

Like what? Well, you know the Scripting Guys: all our suggestions will be dull and boring. You guys can probably come up with something way more exciting. Now, if you’ll excuse us, we’re going to have a glass of warm milk and then take a little nap. We don’t want to get ourselves too worked up, you know.


Discussion is closed.

Feedback usabilla icon