How Can I Create a New Folder in Microsoft Outlook?

How Can I Create a New Folder in Microsoft Outlook?

  • Comments 6
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a new folder in Microsoft Outlook?

-- TY

SpacerHey, Scripting Guy! AnswerScript Center

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 = 6

Set 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 = 6

Set 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 = 6

Set 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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great blog. Just wondering, how would I script creating new folders in an additional mailbox?

    Cheers,

    Adam

  • dude, dis is n awesome code by al means...n i cn't help appreciatin ur genius 2 do d same...wonder tho hw 2 do dis iteratively...m a novice in frnt of u...

    cheers,

    scripter

  • Hi Scripting guy..

    Very new at scripting. I'm trying to add a folder under the inbox (Separate from inbox) in outlook using Exchange. I tried your script but didn't work. Can you help me with this?

    Thanks in advance...

    -Pete

  • Great tip. Struggling to automatically add this folder to the Favorites and setting a Home Page URL to the folder and have it be the default.
    I found a post by Sue Mosher saying:
    Outlook Spy reveals that the MAPI property that contains the information about the folder home page is PR_FOLDER_WEBVIEWINFO (0x36DF0102) and that the binary (Unicode) parses out into these different components:

    dwVersion: 0x00000002
    dwType: 0x00000001
    dwFlags: 0x00000001
    dwUnused: 0x0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
    cbData: 0x36 (00000024)
    bData: http://theurl.com

    The variable portions are dwType, which will be 0 if the home page is on and 1 if it's off, and bData, which is your URL.

    What are your thoughts on how to best change the script to incorporate setting the home page URL and making it the default?

    Thanks

  • 'Set the folders web view
    folder.WebViewURL="http://mypage.dom.com"
    folder.WebViewOn=True

  • Thanks JV.
    I assume I will need Outlook installed to run this script and it will be client side; I won't be able to run this from a server without Outlook installed? Will the Exchange MAPI work? I am leery to test with production environments. :-)
    What about setting the folder in question as a Favorite?
    My quest is nearly complete.