Hey, Scripting Guy! How Can I Delete All the Messages in My Sent Items Folder?

Hey, Scripting Guy! How Can I Delete All the Messages in My Sent Items Folder?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I delete all the messages in my Sent Items folder?

-- OG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, OG. You know, throughout his entire life the Scripting Guy who writes this column has been told that “the only dumb question is the question that goes unasked.” Now, to be perfectly honest, he doesn’t really believe this. For example, when Doug Williams became the first black quarterback to lead his team to the Super Bowl one of the questions that was asked of him was this classic inquiry: “How long have you been a black quarterback?”

Which would seem to support the statement that it isn’t just unasked questions that can be dumb.

Nevertheless, there’s no doubt that a question that doesn’t get asked isn’t going to get answered … unless, of course, you’re a devoted reader of the Hey, Scripting Guy! column. In Hey, Scripting Guy! we not only answer the questions asked of us (sooner or later, anyway) but we also answer the questions that weren’t asked of us. Like what? Like this: yes, it is time for you to start preparing for the 2007 Winter Scripting Games.

That’s right, the 2007 Winter Scripting Games are headed for the Script Center (February 12-23, 2007), and this year the Games are going to be bigger and better than ever: not only do we have way more events than last year, but we also have more divisions (VBScript Beginners; VBScript Advanced; Windows PowerShell Beginners; and Windows PowerShell Advanced). And did we mention that we have prizes this year, some of which have yet to be announced and some of which we have announced? And – brace yourselves – those announced prizes include 250 Dr. Scripto Bobblehead dolls.

Note. Good point: the term Dr.Scripto bobblehead does seem a bit redundant, doesn’t it?

Best of all, all you have to do to have a chance of winning a prize is to enter at least one event; you don’t even have to successfully complete the event. Try naming one other major sporting competition where they randomly choose winners like that!

Well, OK. We mean besides figure skating.

Of course, if you’re anything like the Scripting Guys you probably put off your training until the end, and, suddenly, now the Scripting Games are almost upon us. But, hey, relax. Not ready for the Games? Then download the official Scripting Games Program, featuring tips and tricks, a sample event, and a pair of crossword puzzles designed to get you ready for the Games themselves. You say you’re a scripting novice? Then take a look at our special training article on dealing with arrays. This year there’s no excuse for not trying at least one event in the Scripting Games.

Nope, sorry: no excuse.

OK, now that we’ve taken care of the question no one ever asked let’s see if we can tackle the question that someone did ask. OG, you say you need to delete all the messages in your Sent Items folder? This little script should do just that:

Const olFolderSentMail  = 5

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")

Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)

Set colItems = objFolder.Items

For i = colItems.Count to 1 Step - 1
    colItems(i).Delete
Next

How does this script work? Wow, yet another question we’re going to answer today! To begin with, we start out by creating a constant named olFolderSentMail and setting the value to 5; we’ll use this constant to tell the script which Outlook folder we want to connect to. After defining the constant we create an instance of the Outlook.Application object, then use the GetNamespace method to bind us to the MAPI namespace. As soon as that’s done we use the GetDefaultFolder method to connect to the Sent Items folder, like so:

Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)

And as soon as that’s done we use the following line of code to return a collection of all the messages found in the Sent Items folder:

Set colItems = objFolder.Items

Now it gets a little weird (but just a little). What we need to do next is loop through all the items in the collection and delete each one; to do that we’d usually set up a For Each loop. In Outlook, however, that won’t work; instead, we have to specify the index number of each item when we go to delete it. Furthermore – and for reasons we won’t bother going into – you’ll often run into problems if you try deleting the first message, then the second message, then the third message. Instead, we need to start by deleting the last message, then the next-to-last message, and so on. That’s why we have this odd-looking loop:

For i = colItems.Count to 1 Step - 1
    colItems(i).Delete
Next

As you can see, this loop runs backwards (Step -1) from the total number of messages in the folder (a value we can determine by using the Count property) to the first message in the folder (item 1). This has to do with the fact that each time you delete a message from the collection the individual items in the collection get re-numbered. Because of that, if we start at message 1 and work our way down, we’ll, at best, delete only half the items. If we start at the bottom and work our way up we’ll delete all the items.

We know, we know. But try it yourself and you’ll see what we’re talking about.

Anyway, that should do it, OG. We hope that helps, and we hope to see you – and everyone else – at the Scripting Games.

Note. You’re right: deleting all the items in an Outlook folder would have made for a good Scripting Games event. Dang! Probably too late for that now, isn’t 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
  • how do i delete all items in my sent folder  in a group?

  • thanks