{"id":67093,"date":"2006-06-16T16:06:00","date_gmt":"2006-06-16T16:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/16\/how-can-i-write-a-script-that-accesses-all-the-subfolders-in-my-outlook-inbox\/"},"modified":"2006-06-16T16:06:00","modified_gmt":"2006-06-16T16:06:00","slug":"how-can-i-write-a-script-that-accesses-all-the-subfolders-in-my-outlook-inbox","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-write-a-script-that-accesses-all-the-subfolders-in-my-outlook-inbox\/","title":{"rendered":"How Can I Write a Script That Accesses All the Subfolders in 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 write a script that accesses all the subfolders in my Outlook Inbox?<BR><BR>&#8212; MB<\/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, MB. You know, after you\u2019ve written a daily scripting column for nearly two years (have we mentioned the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/postcards\/default.mspx\"><B>Hey, Scripting Guy 500th column celebration <\/B><\/A>lately?) you learn that certain subjects are strictly off-limits. Discuss religion in a scripting column? Not a good idea. Politics? Don\u2019t even <I>think<\/I> about it. The fact that you should never, <I>ever<\/I> put ice cream on top of cake? Forget it; making a statement like that is bound to create controversy.<\/P>\n<TABLE id=\"EFD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. As long as we\u2019re on the subject, you shouldn\u2019t dip chocolate chip cookies in milk either, at least not if you intend to drink the milk afterwards. <I>(Editor\u2019s Note: In the opinion of this editor, which is really the only opinion that counts, that statement is completely inaccurate. We apologize to all of you who know how to eat cookies properly &#8211; soggy to the point where you can just barely get it to your mouth without it falling into the milk.)<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>If we were smart (yes, yes we know: that\u2019s a big <I>if<\/I>), we\u2019d add recursive functions and subroutines to the list of things that we should never talk about. It\u2019s not that recursive functions and subroutines aren\u2019t useful; they are. (In fact, we have no idea how to answer your question without using recursion.) No, the problem isn\u2019t that recursion is useless, the problem is that recursion is an absolute nightmare to try to explain. And, let\u2019s face it, when you\u2019re a Scripting Guy you have enough nightmares as it is. <I>(Such as editors chasing after Scripting Guys with soggy chocolate chip cookies. Or just editors.)<\/I><\/P>\n<P>On the other hand, it wouldn\u2019t be very good form to start answering a question and then never finish answering it. So, we\u2019ll tell you what, MB: we\u2019ll give you a script that can access all the subfolders in your Inbox, but we won\u2019t promise that we\u2019ll be able to explain the workings of the script in nitty-gritty detail, at least not in today\u2019s column. Deal? Deal.<\/P>\n<TABLE id=\"E1D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. And you <I>can<\/I> get a brief introduction to recursion by taking a peek at the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_vbs_kove.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s start out easy. (OK, <I>relatively<\/I> easy.) Here\u2019s a script that simply returns the names of all the folders and subfolders found in your Outlook Inbox:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)<\/p>\n<p>Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)<\/p>\n<p>Wscript.Echo objInbox.Name<\/p>\n<p>GetSubfolders(objInbox)<\/p>\n<p>Sub GetSubfolders(objParentFolder)\n    Set colFolders = objParentFolder.Folders\n    For Each objFolder in colFolders\n        Set objSubfolder = objParentFolder.Folders(objFolder.Name)\n        Wscript.Echo objFolder.Name\n        GetSubfolders objSubfolder\n    Next\nEnd Sub\n<\/PRE>\n<TABLE id=\"EUE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We should point out that this script assumes that Microsoft Outlook is already running. If that isn\u2019t true (or at least isn\u2019t something you should assume), then check out the <I>Office Space<\/I> article <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/jun05\/tips0614.mspx\"><B>Starting a Script Regardless of Whether or Not Microsoft Outlook is Already Running<\/B><\/A> to find out how to write a script that starts Outlook.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>You\u2019re right: the script <I>does<\/I> start off simple enough, doesn\u2019t it? We begin by defining a constant named olFolderInbox and setting the value to 6; we\u2019ll use this constant to tell the script which Outlook folder we want to bind to. We then use these two lines of code to create an instance of the <B>Outlook.Application<\/B> object and to bind to the <B>MAPI<\/B> namespace (which happens to be the <I>only<\/I> namespace we can bind to):<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\n<\/PRE>\n<P>After we\u2019ve connected to the MAPI namespace we then call the <B>GetDefaultFolder<\/B> method and bind to the Inbox:<\/P><PRE class=\"codeSample\">Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>And after that we simply echo back the name of the Inbox:<\/P><PRE class=\"codeSample\">Wscript.Echo objInbox.Name\n<\/PRE>\n<P>Piece of cake. (Albeit without ice cream on top of it.)<\/P>\n<P>Unfortunately, though, this is where things start to get a little bit tricky. Our next step is to call the recursive subroutine GetSubfolders, passing along the object reference to the Inbox as the subroutine parameter:<\/P><PRE class=\"codeSample\">GetSubfolders(objInbox)\n<\/PRE>\n<P>Here\u2019s what the GetSubfolders subroutine looks like:<\/P><PRE class=\"codeSample\">Sub GetSubfolders(objParentFolder)\n    Set colFolders = objParentFolder.Folders\n    For Each objFolder in colFolders\n        Set objSubfolder = objParentFolder.Folders(objFolder.Name)\n        Wscript.Echo objFolder.Name\n        GetSubfolders objSubfolder\n    Next\nEnd Sub\n<\/PRE>\n<P>Before we do anything else we should point out that, even though we passed the object reference objInbox, GetSubfolders appears to receive something named objParentFolder. Don\u2019t worry about that: objParentFolder is just a variable name we apply to anything that gets passed to the subroutine. (See, we told you this was going to start getting a little weird.) If that bothers you, don\u2019t sweat it: instead, check out the <I>Sesame Script<\/I> column <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/begin\/ss0606.mspx\"><B>Functions, Subroutines, and How to Call Them From Other Scripts<\/B><\/A> for more details.)<\/P>\n<P>Inside the subroutine the first thing we do is use this line of code to create a collection of all the top-level subfolders found in the Inbox:<\/P><PRE class=\"codeSample\">Set colFolders = objParentFolder.Folders\n<\/PRE>\n<P>Is it important to say \u201ctop-level\u201d subfolders instead of just subfolders? Yes, it is. Suppose our Inbox looks like this:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/inbox.jpg\" width=\"179\" height=\"67\"> \n<P><BR>Which of these folders do you suppose are part of the Folders collection? That\u2019s right: only Sample and Test; that\u2019s because they\u2019re the only top-level subfolders in the Inbox. What about Test 2? Well, as a sub-subfolder Test 2 is actually part of the Folders collection for the <I>Test<\/I> folder: to get at sub-subfolders we need to access the Folders collection for all the top-level folders. That\u2019s why we have to use a recursive function: we need to look at a folder, then look at any folders within that folder, then look at any folders within <I>those<\/I> folders. <\/P>\n<P>Etc.<\/P>\n<P>After retrieving the Folders collection we set up a For Each loop to walk through all the items in that collection. Inside that loop we start out by creating an object reference that binds us to the first folder in the collection:<\/P><PRE class=\"codeSample\">Set objSubfolder = objParentFolder.Folders(objFolder.Name)\n<\/PRE>\n<P>After connecting to <I>that<\/I> folder we can echo back the folder name; we can also do this:<\/P><PRE class=\"codeSample\">GetSubfolders objSubfolder\n<\/PRE>\n<P>Yes, we\u2019re calling the subroutine GetSubfolders from inside the GetSubfolders subroutine. Why? Well, if the first folder in the collection is Sample we now need to see whether Sample has any sub-subfolders. This is the part where people\u2019s eyes begin to glaze over, but that\u2019s OK; there\u2019s nothing wrong with simply having faith that this works. It all sounds extremely complicated, and it is. But fortunately VBScript takes care of the hard stuff for us.<\/P>\n<P>When we\u2019re all done we\u2019ll get back a list of <I>every<\/I> folder (top-level or not) found in the Inbox:<\/P><PRE class=\"codeSample\">Inbox\nTest\nTest 2\nSample\n<\/PRE>\n<P>Of course, you didn\u2019t just want the folder names, you wanted to be able to access all the items (e.g., all the mail messages) found in those folders. Hey, no problem. We won\u2019t explain this revised script in any detail; we\u2019ll just note that we use the <B>Items<\/B> collection to retrieve all the items in a folder and then, for each item, we echo back the <B>Subject<\/B> line. Obviously you can modify this script to echo back the sender\u2019s name, the message text, or anything else you want to echo back.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)<\/p>\n<p>Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)<\/p>\n<p>Wscript.Echo objInbox.Name<\/p>\n<p>Set colItems = objInbox.Items<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Subject\nNext<\/p>\n<p>GetSubfolders(objInbox)<\/p>\n<p>Sub GetSubfolders(objParentFolder)\n    Set colFolders = objParentFolder.Folders\n    For Each objFolder in colFolders\n        Set objSubfolder = objParentFolder.Folders(objFolder.Name)\n        Wscript.Echo objFolder.Name<\/p>\n<p>        Set colItems = objFolder.Items<\/p>\n<p>        For Each objItem in colItems\n            Wscript.Echo objItem.Subject\n        Next<\/p>\n<p>        GetSubfolders objSubfolder\n    Next\nEnd Sub\n<\/PRE>\n<P>OK. You kow, that wasn\u2019t as bad as we thought it would be. Maybe tomorrow we <I>will<\/I> tackle the question: is there a God?<\/P>\n<P>Or, then again, maybe we won\u2019t.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write a script that accesses all the subfolders in my Outlook Inbox?&#8212; MB Hey, MB. You know, after you\u2019ve written a daily scripting column for nearly two years (have we mentioned the Hey, Scripting Guy 500th column celebration lately?) you learn that certain subjects are strictly off-limits. Discuss religion [&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-67093","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 write a script that accesses all the subfolders in my Outlook Inbox?&#8212; MB Hey, MB. You know, after you\u2019ve written a daily scripting column for nearly two years (have we mentioned the Hey, Scripting Guy 500th column celebration lately?) you learn that certain subjects are strictly off-limits. Discuss religion [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67093","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=67093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67093\/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=67093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}