May 14th, 2008

Hey, Scripting Guy! How Can I Move Emails From One Outlook Folder to Another?

Hey, Scripting Guy! Question

Hey, Scripting Guy! You’ve had several articles that show how it’s possible to find specific email messages in Outlook; for example, you’ve shown us how to find all the emails sent by a particular person. What I was wondering is this: after you find these emails, is it possible to have the script move the messages to a different folder?
— FD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, FD. Before we begin we’d like to point out that the Seattle Mariners did win their game on Sunday, defeating the Chicago White Sox 6-3. What does that mean? That means that all of you who said that the Mariners probably wouldn’t win another game all season were wrong. Dead wrong.

What’s that? Does this mean that the Mariners might even win another game before the season is over? Well, let’s not get carried away here; after all, the team is fully capable of putting a lineup out on the field that includes 5 players hitting .208 or worse.

Note. For those of you who are not baseball fans, that’s not good at all. For those of you who are baseball fans, well, that is hard to believe, isn’t it? But, sadly enough, it’s true. Kenji Johjima recently signed a three-year, $24 million contract. Kenji is currently batting .208 with no home runs and just 5 runs batted-in.

And yes, now that you mention it, the Scripting Guy who writes this column could hit no home runs for the Seattle Mariners. And he’d be willing to do that for, say, $5 million a year rather than $8 million.

Of course, this hasn’t exactly been a banner year for Seattle sports teams. The University of Washington football team started their season off by beating Syracuse on the road, then surprising Boise State at home. After that … well, we can’t remember what happened after that. As for the Husky basketball team … well, we can’t remember much about the basketball season, either. The Seattle Seahawks did make the playoffs, but no one seriously considered them a Super Bowl contender. As for our professional basketball team, the Seattle Sonics, they’re currently sitting on the curb waiting for the bus to whisk them off to Oklahoma City.

Which means at least one good thing happened this year in Seattle sports.

Note to the good people of Oklahoma City. You recently voted to spend $100 million fixing up your sports arena to make room for the Sonics. Did anyone mention that the Sonics won only 20 games last year, and that the team had one losing streak of 14 games and another of 11 games?

And you’re right: we probably should have told you that earlier. Sorry. But it’s too late to give them back now.

In other words, when it comes to sports in the Seattle area about the only thing you can count on is that you can’t count on anything. But do you know who you can always count on in the Seattle? Actually we were hoping that you did know; we couldn’t think of anyone that you can always count on, not in the Seattle area anyway.

On the other hand, we do know someone you can occasionally count on: the Scripting Guys. (That’s us, by the way.) Just to prove it, here’s a script that can locate all the emails sent by a specified user, and then move those messages out of the Inbox and into a different folder:

On Error Resume Next

Const olFolderInbox = 6

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

strFolderName = objInbox.Parent
Set objMailbox = objNamespace.Folders(strFolderName)
Set objFolder = objMailbox.Folders("Ken Myer Folder")

Set colItems = objInbox.Items
Set objItem = colItems.Find("[SenderName] = 'Ken Myer'")

Do While TypeName(objItem) <> "Nothing"
    objItem.Move objFolder
    Set objItem = colItems.FindNext
Loop

What’s that? Can we show you how this script works? Of course we can; didn’t we just say that you can always count on the Scripting Guys?

Oh, well, good point: we didn’t just say that, did we? But that’s OK; we’ll explain how the script works anyway.

As you can see, we kick things off by defining a constant named olFolderInbox and setting the value to 6; we’ll use this constant to direct the script to the Outlook Inbox. After defining the constant, we create an instance of the Outlook.Application object, then use the GetNamespace method to bind to the MAPI namespace. (Even though that’s the only namespace you can bind to, we still have to call GetNamespace and explicitly bind to it.) Once that’s done we employ the following line of code to connect to the Inbox folder:

Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)

For our scenario, we’re assuming that we have a folder named Ken Myer Folder; this folder happens to be a sibling of the Inbox folder. (In other words, it’s not a subfolder of the Inbox folder.) Because our target folder is a sibling of the Inbox, we use this block of code to create an object reference to that folder (oh, by the way: we need to create an object reference to that folder):

strFolderName = objInbox.Parent
Set objMailbox = objNamespace.Folders(strFolderName)
Set objFolder = objMailbox.Folders("Ken Myer Folder")

So what’s going on here? Well, in the first line we’re using the Parent property to determine the “parent” folder for the Inbox; that’s simply going to be the Mailbox folder. After we have the Parent folder path we use line 2 to bind to the Mailbox folder at that path:

Set objMailbox = objNamespace.Folders(strFolderName)

And once we’re connected to the Mailbox we can then create our object reference by using the Mailbox object’s Folders collection, like so:

Set objFolder = objMailbox.Folders("Ken Myer Folder")

In other words, we simply specify a particular folder (Ken Myer Folder) within the collection and we’ll have our object reference.

Now, what if our target folder was a subfolder of the Inbox? In that case our task is much easier; in fact, in that case we can replace the previous three lines of code with this:

Set objFolder = objInbox.Folders("Ken Myer Folder")

Because we’re already connected to the Inbox we don’t have to worry about the Mailbox folder; instead, we use the Inbox object’s Folders collection. And we can bind directly to that because we already have an object reference (objInbox) that points to the Inbox folder.

Make sense? Excellent.

After we create the object reference to the target folder (that is, the folder where we want to move the specified messages) we next use this line of code to return a collection of all the items found in the Inbox:

Set colItems = objInbox.Items

Good point: we really don’t have much use for all the items found in the Inbox, do we? Maybe that’s why we tacked on this line of code, a line of code that tells the script to search through the collection and look for the first email in which the SenderName property is equal to Ken Myer:

Set objItem = colItems.Find("[SenderName] = 'Ken Myer'")

Our next step is to set up a Do While loop that will continue to run as long as we keep finding email sent by ken Myer. How do we know whether or not the script found an email sent by Ken Myer? Well, one simple way is to use the TypeName property to verify that the data type of the variable objItem is not equal to Nothing:

Do While TypeName(objItem) <> "Nothing"

If the data type of our variable is equal to Nothing that means that we have, well, nothing; at that point, our loop will automatically come to an end. If the data type isn’t equal to Nothing that means that we have, well, something. In fact, that means we must have found a message sent by Ken Myer. With that in mind, we use the Move method to move the message to the target folder:

objItem.Move objFolder

And then we simply call the FindNext method to find the next such item in the collection.

And yes, instead of using Find and FindNext we could have used a filter to limit our collection to emails sent by Ken Myer. Why didn’t we? Well, working with a filtered collection can be a bit tricky; for an example, see this column on deleting items from Microsoft Outlook. Because of that, we thought we’d try a different approach: find an item, move it, then find the next item and move that. That seemed a little more straightforward to us. But if you prefer to work with a filtered collection, well, there’s nothing wrong with that. You know what they say: live like you want to live, baby!

That’s really all there is to it, FD; we hope that helps. And that’s about all there is to the Seattle area sports scene as well. Well, OK, we should mention that the University of Washington women’s softball team did qualify for the NCAA tournament, their 15th consecutive year in the NCAA tourney. As Husky coach Heather Carr noted, “We are coming out of the Pac-10 season with some momentum and look forward to traveling to Houston for a great tournament venue.” How much momentum are the Huskies coming out of the Pac-10 season with? As it turns out, the team lost 12 of its 16 games, all of which were Pac-10 Conference games.

That pretty much tells you everything you need to know about the current state of Seattle-area sports.

Author

0 comments

Discussion are closed.