{"id":66193,"date":"2006-10-24T12:49:00","date_gmt":"2006-10-24T12:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/24\/how-can-i-connect-to-the-junk-mail-folder-in-outlook\/"},"modified":"2006-10-24T12:49:00","modified_gmt":"2006-10-24T12:49:00","slug":"how-can-i-connect-to-the-junk-mail-folder-in-outlook","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-connect-to-the-junk-mail-folder-in-outlook\/","title":{"rendered":"How Can I Connect to the Junk Mail Folder in Outlook?"},"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! Over the past year or so you\u2019ve answered several questions in which you use a script to connect to the Inbox in Microsoft Outlook. But how can I connect to the Junk Mail folder in Outlook?<BR><BR>&#8212; ME<\/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, ME. Let\u2019s see, what time <I>is<\/I> it? OK good: we have enough time to answer this question and get the column posted. We were a little worried about that, because the Scripting Guy who writes this column has been running late all day today. But that\u2019s not entirely his fault; for example, after taking a shower this morning he was drying himself off when his eye caught this notice on the bath towel label:<\/P>\n<P><B>Not suitable for wearing apparel.<\/B><\/P>\n<P>A bath towel, by itself, is not suitable for wearing apparel?!? Why didn\u2019t someone tell him that <I>earlier<\/I>? As you might expect, that forced him to rethink his wardrobe for the day, and got him off to a bad start before the day ever really got started.<\/P>\n<TABLE id=\"EHD\" 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>. Although, admittedly, rethinking his wardrobe isn\u2019t really that big of a deal; most days that simply involves picking between the black hooded-sweatshirt and the grey hooded-sweatshirt. Today he went with grey. Tomorrow? Who knows?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Fortunately, the Scripting Guy who writes this column was able to make it to work on time, and \u2013 for once \u2013 showed up suitably attired. In turn, that gave him enough time to come up with a simple little demonstration script that connects to the Junk Mail folder in Outlook and reports back the number of items in that folder:<\/P><PRE class=\"codeSample\">Const olFolderJunk = 23<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderJunk)<\/p>\n<p>Set colItems = objFolder.Items\nWscript.Echo colItems.Count\n<\/PRE>\n<P>Pretty simple, isn\u2019t it? Not only that, but the key to this script comes in the very first line, the line where we define a constant named olFolderJunk and set the value to 23:<\/P><PRE class=\"codeSample\">Const olFolderJunk = 23\n<\/PRE>\n<P>As you know, Outlook contains a number of predefined folders, including the Inbox, the Calendar, the Contacts folder, and, of course, the Junk Mail folder. Connecting to one of these folders \u2013 as we\u2019re about to see \u2013 is as simple as passing the appropriate value to the <B>GetDefaultFolder<\/B> method. For example, to bind to the Junk Mail folder you need to pass the value 23 (either directly, or by using a constant) to GetDefaultFolder.<\/P>\n<TABLE id=\"E2D\" 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>. Is it just a coincidence that 23 is also the number of flavors blended together to make Dr. Pepper? Unfortunately, we can\u2019t help you there; that\u2019s the sort of thing everyone will have to decide for themselves.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we define the constant olFolderJunk we create an instance of the <B>Outlook.Application<\/B> object and then use the <B>GetNamespace<\/B> method to bind to the <B>MAPI<\/B> namespace. (Which, interestingly enough, is the <I>only<\/I> namespace we can bind to.) We then use this line of code to create an object reference to the Junk Mail folder:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderJunk)\n<\/PRE>\n<P>That\u2019s basically the whole script right there. In the last two lines of code we retrieve a collection of all the items in the Junk Mail folder and then echo back the value of the <B>Count<\/B> property; that simply tells us how many junk emails are currently in the folder. Obviously that\u2019s not required; we added that code simply so the script would do something besides bind to the folder. Alternatively, we could have used a script like this one to go ahead and delete all the items in the folder:<\/P><PRE class=\"codeSample\">Const olFolderJunk = 23<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderJunk)<\/p>\n<p>Set colItems = objFolder.Items<\/p>\n<p>For Each objItem in colItems\n    objItem.Delete\nNext\n<\/PRE>\n<P>Cool, huh?<\/P>\n<P>In case you\u2019re wondering here are the constants (and their corresponding values) for the other default folders built into Microsoft Outlook:<\/P>\n<TABLE id=\"E4E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderCalendar<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">9<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderContacts<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">10<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderDeletedItems<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderDrafts<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">16<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderInbox<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderJournal<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">11<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderJunk<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">23<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderNotes<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">12<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderOutbox<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderSentMail<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderTasks<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">13<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olPublicFoldersAllPublicFolders<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">18<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderConflicts<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">19<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderLocalFailures<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">21<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderServerFailures<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">22<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">olFolderSyncIssues<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">20<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what are we supposed to <I>do<\/I> with all this information? Well, according to the table, the Drafts folder has a value of 16. That means a simple little script like this will tell you how many items are in the Drafts folder:<\/P><PRE class=\"codeSample\">Const olFolderDrafts = 16<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderDrafts)<\/p>\n<p>Set colItems = objFolder.Items\nWscript.Echo colItems.Count\n<\/PRE>\n<P>Etc., etc.<\/P>\n<P>By the way, we have a confession to make: the Scripting Guy who writes this column didn\u2019t <I>really<\/I> consider wearing a towel to work today. After all, thanks to the last such \u2026 incident \u2026 one of his fellow Scripting Guys always calls every morning to remind him that a towel is not suitable for wearing apparel. He learned his lesson, the hard way.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Over the past year or so you\u2019ve answered several questions in which you use a script to connect to the Inbox in Microsoft Outlook. But how can I connect to the Junk Mail folder in Outlook?&#8212; ME Hey, ME. Let\u2019s see, what time is it? OK good: we have enough time to [&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-66193","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! Over the past year or so you\u2019ve answered several questions in which you use a script to connect to the Inbox in Microsoft Outlook. But how can I connect to the Junk Mail folder in Outlook?&#8212; ME Hey, ME. Let\u2019s see, what time is it? OK good: we have enough time to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66193","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=66193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66193\/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=66193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}