Hey, Scripting Guy! Over the past year or so you’ve 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?
— ME
Hey, ME. Let’s see, what time is 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’s 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:
Not suitable for wearing apparel.
A bath towel, by itself, is not suitable for wearing apparel?!? Why didn’t someone tell him that earlier? 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.
Note. Although, admittedly, rethinking his wardrobe isn’t 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? |
Fortunately, the Scripting Guy who writes this column was able to make it to work on time, and – for once – 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:
Const olFolderJunk = 23Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderJunk)
Set colItems = objFolder.Items Wscript.Echo colItems.Count
Pretty simple, isn’t 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:
Const olFolderJunk = 23
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 – as we’re about to see – is as simple as passing the appropriate value to the GetDefaultFolder 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.
Note. Is it just a coincidence that 23 is also the number of flavors blended together to make Dr. Pepper? Unfortunately, we can’t help you there; that’s the sort of thing everyone will have to decide for themselves. |
After we define the constant olFolderJunk we create an instance of the Outlook.Application object and then use the GetNamespace method to bind to the MAPI namespace. (Which, interestingly enough, is the only namespace we can bind to.) We then use this line of code to create an object reference to the Junk Mail folder:
Set objFolder = objNamespace.GetDefaultFolder(olFolderJunk)
That’s 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 Count property; that simply tells us how many junk emails are currently in the folder. Obviously that’s 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:
Const olFolderJunk = 23Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderJunk)
Set colItems = objFolder.Items
For Each objItem in colItems objItem.Delete Next
Cool, huh?
In case you’re wondering here are the constants (and their corresponding values) for the other default folders built into Microsoft Outlook:
Constant |
Value |
olFolderCalendar |
9 |
olFolderContacts |
10 |
olFolderDeletedItems |
3 |
olFolderDrafts |
16 |
olFolderInbox |
6 |
olFolderJournal |
11 |
olFolderJunk |
23 |
olFolderNotes |
12 |
olFolderOutbox |
4 |
olFolderSentMail |
5 |
olFolderTasks |
13 |
olPublicFoldersAllPublicFolders |
18 |
olFolderConflicts |
19 |
olFolderLocalFailures |
21 |
olFolderServerFailures |
22 |
olFolderSyncIssues |
20 |
So what are we supposed to do 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:
Const olFolderDrafts = 16Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderDrafts)
Set colItems = objFolder.Items Wscript.Echo colItems.Count
Etc., etc.
By the way, we have a confession to make: the Scripting Guy who writes this column didn’t really consider wearing a towel to work today. After all, thanks to the last such … incident … 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.
0 comments