Hey, Scripting Guy! How can I create a new folder in Microsoft Outlook?
— TY
Hey, TY. You know, this is an exciting time here at Microsoft; after all, last night at midnight we officially released one of the most highly-anticipated software products ever created: a script that can create a new folder in Microsoft Outlook!
Oh, wait; sorry. We’re releasing that script right now. Last night we released some video game. Halo Something-Or-Other.
No, hey, just kidding: even people as out-of-it as the Scripting Guys know about Halo 3. After all, if you watch TV, read magazines, drink soda or do just about anything then you can’t help but know about Halo 3.
And the Scripting Guys watch a lot of TV and drink a lot of soda.
In case you’re wondering, however, the answer is: no, the Scripting Guy who writes this column did not stand in line at midnight in order to be one of the first people to receive a copy of Halo 3. To be honest, the Scripting Guy who writes this column will probably never even play Halo 3, let alone get his very own copy of it. Why won’t he play Halo 3? Well, a few years ago the Scripting Guy who writes this column got talked into playing the original Halo with his then-13-year-old son and his two nephews. The boys are still laughing about that.
How bad could that have been? Well, shortly after killing his own teammate – by accident, we hasten to add – the Scripting Guy who writes this column stumbled upon some flying vehicle, a flying vehicle armed with deadly missiles or bombs or something. “Oh, you are so lucky,” said the Scripting Son. “You can’t possibly lose now.”
And here we thought the Scripting Son knew his father better than that.
Note. Does that mean that the Scripting Dad had problems flying this vehicle? No, he had trouble just getting into the vehicle. Regardless of which button or combination of buttons he pushed, he could never get onboard. “Dad, just press the X button,” the Scripting Son kept saying. “I am pressing the X button!” the Scripting Dad kept replying. “Wait: which one is the X button?” Fortunately one of the nephews came by and, to put it euphemistically, put the Scripting Dad out of his misery. |
Besides, last night at midnight we were standing in line to get our hands on this baby: Microsoft Outlook Folder Creator 1. At the risk of making everyone out there jealous, here’s what the script looks like (you can look, but don’t touch):
Const olFolderInbox = 6Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set objNewFolder = objFolder.Folders.Add(“Scripts Received”)
Granted, this has nowhere near as many lines of code as Halo 3, and the graphics aren’t quite as nice, either. But, then again, how many Microsoft Outlook folders can you create using Halo 3?
Really? That many, huh? Wow; we never would have guessed that.
Note. Well, if nothing else our script is still cheaper than Halo 3. Although, admittedly, that’s only because Microsoft won’t let us charge people to read this column. |
Let’s see if we can figure out how Microsoft Outlook Folder Creator 1 works. We start out by defining a constant named olFolderInbox and setting the value to 6; we’ll use this constant when we bind to the Inbox folder in Microsoft Outlook. After defining the constant, we create an instance of the Outlook.Application object, then use the GetNamespace method to bind to the MAPI namespace. (And yes, the MAPI namespace is the only namespace you can bind to. Nevertheless, you still have to call the GetNamespace method and make the connection.)
From there we use this line of code to return an object reference (objFolder) to the Inbox folder:
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Once we’ve done that we can create a new folder simply by calling the Add method (part of the Inbox’s Folders collection), passing the name of the new folder as the sole method parameter:
Set objNewFolder = objFolder.Folders.Add(“Scripts Received”)
Just like that our Inbox has a new subfolder named Scripts Received.
Note. At the risk of tooting our own horn, Halo 3 won’t come right out and tell you how it ends; you have to play – and win – the game before you can find out the dramatic conclusion. The Scripting Guys would never make you do that; instead, we tell you flat out exactly how our script ends: it creates a new folder named Scripts Received. |
As we noted, our new folder is a subfolder of the Inbox. That’s fine … unless you wanted this new folder to be at the same level as the Inbox (along with folders like Deleted Items, Junk Mail, and Sent Items). If that’s the case, well, too bad; you’re just going to have to wait until Microsoft Outlook Folder Creator 2 is released.
Fortunately, that release date turns out to be right about now:
Const olFolderInbox = 6Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
strFolderName = objFolder.Parent Set objMailbox = objNamespace.Folders(strFolderName)
Set objNewFolder = objMailbox.Folders.Add(“Scripts Received”)
The tricky part with this script is binding to the Outlook Mailbox as opposed to the Inbox. As far as we know, there’s no straightforward way to bind to the Mailbox; we have no idea what constant value would be required to do that. But as everyone knows, all great video games have their cheat codes, and Microsoft Outlook Folder Creator is no exception. Can’t bind directly to the Mailbox? That’s OK; we’ll just bind to the Inbox and then retrieve the value of the Inbox’s Parent folder:
strFolderName = objFolder.Parent
Once that value has been stashed in the variable strFolderName we can then use it to create an object reference to the Mailbox folder:
Set objMailbox = objNamespace.Folders(strFolderName)
That’s nice, but how do we create a new subfolder of the Mailbox folder? Pretty much the same way we created a new subfolder of the Inbox folder:
Set objNewFolder = objMailbox.Folders.Add(“Scripts Received”)
That’s all there is to it.
Now, we know what you’re thinking. You’re thinking, “Yeah, that’s all well and good, but Halo 3 has a reset button. If things ever go wrong I can just press the reset button and start all over again. I bet Microsoft Outlook Folder Creator can’t do that, can it?”
Oh, can’t it? Take a look at this little block of code:
Const olFolderInbox = 6Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
strFolderName = objFolder.Parent Set objMailbox = objNamespace.Folders(strFolderName)
Set objNewFolder = objMailbox.Folders(“Scripts Received”) objNewFolder.Delete
You’re right: this does look very similar to the script that creates a subfolder in the Mailbox folder, doesn’t it? However, there are a couple of important differences. For example, after we bind to the Mailbox folder, we use this line of code to create an object reference to the Scripts Received subfolder:
Set objNewFolder = objMailbox.Folders(“Scripts Received”)
From there we then call the Delete method to remove the folder:
objNewFolder.Delete
At that point, just like Halo 3, we can start all over again.
We hope that answers your question, TY. In the meantime, we’ve got work to do: we’re still negotiating with the Mountain Dew people to create a limited edition Microsoft Outlook Folder Creator-brand Mountain Dew. How are those negotiations going? To be honest, not quite as well as we had hoped. But we’re working on it.
0 comments