{"id":55573,"date":"2008-05-14T01:40:00","date_gmt":"2008-05-14T01:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/05\/14\/hey-scripting-guy-how-can-i-move-emails-from-one-outlook-folder-to-another\/"},"modified":"2008-05-14T01:40:00","modified_gmt":"2008-05-14T01:40:00","slug":"hey-scripting-guy-how-can-i-move-emails-from-one-outlook-folder-to-another","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-move-emails-from-one-outlook-folder-to-another\/","title":{"rendered":"Hey, Scripting Guy! How Can I Move Emails From One Outlook Folder to Another?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! You\u2019ve had several articles that show how it\u2019s possible to find specific email messages in Outlook; for example, you\u2019ve 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?<br \/>&#8212; FD<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, FD. Before we begin we\u2019d like to point out that the Seattle Mariners <i>did<\/i> 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\u2019t win another game all season were wrong. <i>Dead<\/i> wrong.<\/p>\n<p>What\u2019s that? Does this mean that the Mariners might even win <i>another<\/i> game before the season is over? Well, let\u2019s not get carried away here; after all, the team <i>is<\/i> fully capable of putting a lineup out on the field that includes 5 players hitting .208 or worse.<\/p>\n<table class=\"dataTable\" id=\"EJD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. For those of you who are not baseball fans, that\u2019s not good at all. For those of you who <i>are<\/i> baseball fans, well, that <i>is<\/i> hard to believe, isn\u2019t it? But, sadly enough, it\u2019s 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.<\/p>\n<p>And yes, now that you mention it, the Scripting Guy who writes this column <i>could<\/i> hit no home runs for the Seattle Mariners. And he\u2019d be willing to do that for, say, $5 million a year rather than $8 million.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, this hasn\u2019t 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 \u2026 well, we can\u2019t remember what happened after that. As for the Husky basketball team \u2026 well, we can\u2019t remember much about the basketball season, either. The Seattle Seahawks <i>did<\/i> make the playoffs, but no one seriously considered them a Super Bowl contender. As for our professional basketball team, the Seattle Sonics, they\u2019re currently sitting on the curb waiting for the bus to whisk them off to Oklahoma City.<\/p>\n<p>Which means at least <i>one<\/i> good thing happened this year in Seattle sports.<\/p>\n<table class=\"dataTable\" id=\"EJE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note to the good people of Oklahoma City<\/b>. 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?<\/p>\n<p>And you\u2019re right: we probably <i>should<\/i> have told you that earlier. Sorry. But it\u2019s too late to give them back now.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In other words, when it comes to sports in the Seattle area about the only thing you can count on is that you can\u2019t count on anything. But do you know who you can <i>always<\/i> count on in the Seattle? Actually we were hoping that you <i>did<\/i> know; we couldn\u2019t think of anyone that you can always count on, not in the Seattle area anyway.<\/p>\n<p>On the other hand, we do know someone you can <i>occasionally<\/i> count on: the Scripting Guys. (That\u2019s us, by the way.) Just to prove it, here\u2019s 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:<\/p>\n<pre class=\"codeSample\">On Error Resume Next\n\nConst olFolderInbox = 6\n\nSet objOutlook = CreateObject(\"Outlook.Application\")\nSet objNamespace = objOutlook.GetNamespace(\"MAPI\")\nSet objInbox = objNamespace.GetDefaultFolder(olFolderInbox)\n\nstrFolderName = objInbox.Parent\nSet objMailbox = objNamespace.Folders(strFolderName)\nSet objFolder = objMailbox.Folders(\"Ken Myer Folder\")\n\nSet colItems = objInbox.Items\nSet objItem = colItems.Find(\"[SenderName] = 'Ken Myer'\")\n\nDo While TypeName(objItem) &lt;&gt; \"Nothing\"\n    objItem.Move objFolder\n    Set objItem = colItems.FindNext\nLoop\n<\/pre>\n<p>What\u2019s that? Can we show you how this script works? Of course we can; didn\u2019t we just say that you can always count on the Scripting Guys?<\/p>\n<p>Oh, well, good point: we <i>didn\u2019t<\/i> just say that, did we? But that\u2019s OK; we\u2019ll explain how the script works anyway.<\/p>\n<p>As you can see, we kick things off by defining a constant named olFolderInbox and setting the value to 6; we\u2019ll use this constant to direct the script to the Outlook Inbox. After defining the constant, we create an instance of the <b>Outlook.Application<\/b> object, then use the <b>GetNamespace<\/b> method to bind to the MAPI namespace. (Even though that\u2019s the only namespace you <i>can<\/i> bind to, we still have to call GetNamespace and explicitly bind to it.) Once that\u2019s done we employ the following line of code to connect to the Inbox folder:<\/p>\n<pre class=\"codeSample\">Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/pre>\n<p>For our scenario, we\u2019re assuming that we have a folder named <i>Ken Myer Folder<\/i>; this folder happens to be a sibling of the Inbox folder. (In other words, it\u2019s 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):<\/p>\n<pre class=\"codeSample\">strFolderName = objInbox.Parent\nSet objMailbox = objNamespace.Folders(strFolderName)\nSet objFolder = objMailbox.Folders(\"Ken Myer Folder\")\n<\/pre>\n<p>So what\u2019s going on here? Well, in the first line we\u2019re using the <b>Parent<\/b> property to determine the \u201cparent\u201d folder for the Inbox; that\u2019s 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:<\/p>\n<pre class=\"codeSample\">Set objMailbox = objNamespace.Folders(strFolderName)\n<\/pre>\n<p>And once we\u2019re connected to the Mailbox we can then create our object reference by using the Mailbox object\u2019s <b>Folders<\/b> collection, like so:<\/p>\n<pre class=\"codeSample\">Set objFolder = objMailbox.Folders(\"Ken Myer Folder\")\n<\/pre>\n<p>In other words, we simply specify a particular folder (Ken Myer Folder) within the collection and we\u2019ll have our object reference.<\/p>\n<p>Now, what if our target folder <i>was<\/i> 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:<\/p>\n<pre class=\"codeSample\">Set objFolder = objInbox.Folders(\"Ken Myer Folder\")\n<\/pre>\n<p>Because we\u2019re already connected to the Inbox we don\u2019t have to worry about the Mailbox folder; instead, we use the <i>Inbox<\/i> object\u2019s Folders collection. And we can bind directly to that because we already have an object reference (objInbox) that points to the Inbox folder.<\/p>\n<p>Make sense? Excellent.<\/p>\n<p>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:<\/p>\n<pre class=\"codeSample\">Set colItems = objInbox.Items\n<\/pre>\n<p>Good point: we really don\u2019t have much use for <i>all<\/i> the items found in the Inbox, do we? Maybe that\u2019s 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 <b>SenderName<\/b> property is equal to <i>Ken Myer<\/i>:<\/p>\n<pre class=\"codeSample\">Set objItem = colItems.Find(\"[SenderName] = 'Ken Myer'\")\n<\/pre>\n<p>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 <b>TypeName<\/b> property to verify that the data type of the variable objItem is not equal to Nothing:<\/p>\n<pre class=\"codeSample\">Do While TypeName(objItem) &lt;&gt; \"Nothing\"\n<\/pre>\n<p>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 <i>isn\u2019t<\/i> 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 <b>Move<\/b> method to move the message to the target folder:<\/p>\n<pre class=\"codeSample\">objItem.Move objFolder\n<\/pre>\n<p>And then we simply call the <b>FindNext<\/b> method to find the next such item in the collection.<\/p>\n<p>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\u2019t we? Well, working with a filtered collection can be a bit tricky; for an example, see this column on <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0629.mspx\"><b>deleting items<\/b><\/a> from Microsoft Outlook. Because of that, we thought we\u2019d 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\u2019s nothing wrong with that. You know what they say: live like you want to live, baby!<\/p>\n<p>That\u2019s really all there is to it, FD; we hope that helps. And that\u2019s about all there is to the Seattle area sports scene as well. Well, OK, we should mention that the University of Washington women\u2019s softball team <i>did<\/i> qualify for the NCAA tournament, their 15<sup>th<\/sup> consecutive year in the NCAA tourney. As Husky coach Heather Carr noted, \u201cWe are coming out of the Pac-10 season with some momentum and look forward to traveling to Houston for a great tournament venue.\u201d How <i>much<\/i> 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.<\/p>\n<p>That pretty much tells you everything you need to know about the current state of Seattle-area sports.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! You\u2019ve had several articles that show how it\u2019s possible to find specific email messages in Outlook; for example, you\u2019ve 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 [&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-55573","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! You\u2019ve had several articles that show how it\u2019s possible to find specific email messages in Outlook; for example, you\u2019ve 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55573","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=55573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55573\/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=55573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}