{"id":66763,"date":"2006-08-03T10:37:00","date_gmt":"2006-08-03T10:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/03\/how-can-i-get-access-to-a-mail-folder-that-isnt-a-subfolder-of-my-outlook-inbox\/"},"modified":"2006-08-03T10:37:00","modified_gmt":"2006-08-03T10:37:00","slug":"how-can-i-get-access-to-a-mail-folder-that-isnt-a-subfolder-of-my-outlook-inbox","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-access-to-a-mail-folder-that-isnt-a-subfolder-of-my-outlook-inbox\/","title":{"rendered":"How Can I Get Access to a Mail Folder That Isn\u2019t a Subfolder of My Outlook Inbox?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I access a specified mail folder in my Outlook Mailbox, a folder that <I>isn\u2019t<\/I> a subfolder of my Inbox?<BR><BR>&#8212; RT<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RT. As you recall, in our <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/aug06\/hey0801.mspx\"><B>last exciting episode<\/B><\/A> \u2013 what do you mean you don\u2019t recall <I>any<\/I> exciting episodes when it comes to this column? Come on: this is <I>Hey, Scripting Guy!<\/I>, the <A href=\"http:\/\/en.wikipedia.org\/wiki\/Kingda_Ka\" target=\"_blank\"><B>Kingda Ka<\/B><\/A> of the Scripting World! Remember that episode where we showed people how they could <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0627.mspx\"><B>create 150 copies of a single file<\/B><\/A>? You\u2019re telling us <I>that<\/I> didn\u2019t have you on the edge of your seats? <\/P>\n<P>OK, true. But what about that episode where we \u2013 well, come to think of it, that really <I>wasn\u2019t<\/I> all that exciting, was it? But then there was the time that we \u2013 well, no: that was useful, but it wasn\u2019t exactly a thrill-a-minute. Let\u2019s see what we have here: <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0607.mspx\"><B>adding leading zeroes to all the lines in a text file<\/B><\/A>; <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/may06\/hey0517.mspx\"><B>renaming a local user account on a Windows XP computer<\/B><\/A>; <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/apr06\/hey0403.mspx\"><B>counting the number of words in a text file<\/B><\/A> \u2013 you know, maybe you\u2019re right. Let\u2019s start over.<\/P>\n<P>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\u2019t a subfolder of your Inbox. What do we mean by that? Well, consider the following Outlook folder structure:<\/P><IMG border=\"0\" alt=\"Microsoft Outlook\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/mailbox.jpg\" width=\"173\" height=\"461\"> \n<P><BR>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\u2019s great, except for one thing: how the heck do you access a folder that isn\u2019t a subfolder of the Inbox?<\/P>\n<P>Here\u2019s how:<\/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)<\/p>\n<p>Set objFolder = objMailbox.Folders(&#8220;Europe&#8221;)<\/p>\n<p>Set colItems = objFolder.Items<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Subject\nNext\n<\/PRE>\n<P>Granted, if this was an <I>exciting<\/I> column we\u2019d let you enjoy the thrill and adventure of trying to figure out for yourself how this script works. But seeing as how this <I>isn\u2019t<\/I> an exciting column we\u2019ll go ahead and explain it all for you.<\/P>\n<P>To begin with, we define a constant named olFolderInbox and set the value to 6. Now, don\u2019t panic: we know that we aren\u2019t interested in the Inbox or any of the subfolders of the Inbox. We\u2019ll explain why we need this constant in just a second.<\/P>\n<P>After defining the constant we create an instance of the <B>Outlook.Application<\/B> object and use the <B>GetNamespace<\/B> method to bind to the MAPI namespace (the only namespace we <I>can<\/I> bind to). We then use this line of code to bind to the Inbox folder:<\/P><PRE class=\"codeSample\">Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>No, we didn\u2019t forget: there\u2019s a reason we start out by binding to the Inbox. In this script we\u2019re trying to retrieve information from the Europe folder, which happens to be a subfolder of the Mailbox \u2013 Ken Myer folder. In order to get to the Europe folder we need to first bind to Mailbox \u2013 Ken Myer; we can bind to the Europe folder only after we\u2019ve done that. We need to work our way through the folder structure, one level at a time.<\/P>\n<P>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 \u2013 Ken Myer. That\u2019s great. But suppose you share this script with someone else. In that case, the odds are pretty slim that <I>their<\/I> mailbox will also be named Mailbox \u2013 Ken Myer. What we\u2019re trying to do here is develop a script that can connect to <I>any<\/I> mailbox folder, without having to hardcode the mailbox name into the code.<\/P>\n<P>Make sense? The only problem with that is the fact that we don\u2019t know of any way to directly retrieve the mailbox name. (We\u2019re not saying that there isn\u2019t such a way; we just don\u2019t know what it is.) However, we <I>do<\/I> know that the mailbox name can be retrieved by binding to the Inbox and then grabbing the value of the <B>Parent<\/B> property. Because the Inbox is a child folder of the mailbox, the Inbox Parent will be \u2013 tah-dah! \u2013 the mailbox. That\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">strFolderName = objInbox.Parent\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">Set objMailbox = objNamespace.Folders(strFolderName)\n<\/PRE>\n<P>Was that worth the effort? You bet it was; after all, once we\u2019re 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:<\/P><PRE class=\"codeSample\">Set objFolder = objMailbox.Folders(&#8220;Europe&#8221;)\n<\/PRE>\n<P>See how that works? We simply reference the mailbox <B>Folders<\/B> 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.<\/P>\n<P>From that point on it\u2019s a piece of cake. To retrieve a collection of all the items (email messages) in the Europe folder we use this line of code:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>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 <B>Subject<\/B> of each message, although once you have this collection you can do pretty much anything you want with it. <\/P>\n<P>Like what? Well, you know the Scripting Guys: all our suggestions will be dull and boring. You guys can probably come up with something <I>way<\/I> more exciting. Now, if you\u2019ll excuse us, we\u2019re going to have a glass of warm milk and then take a little nap. We don\u2019t want to get ourselves <I>too<\/I> worked up, you know.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I access a specified mail folder in my Outlook Mailbox, a folder that isn\u2019t a subfolder of my Inbox?&#8212; RT Hey, RT. As you recall, in our last exciting episode \u2013 what do you mean you don\u2019t recall any exciting episodes when it comes to this column? Come on: this [&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-66763","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 access a specified mail folder in my Outlook Mailbox, a folder that isn\u2019t a subfolder of my Inbox?&#8212; RT Hey, RT. As you recall, in our last exciting episode \u2013 what do you mean you don\u2019t recall any exciting episodes when it comes to this column? Come on: this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66763","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=66763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66763\/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=66763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}