{"id":66833,"date":"2006-07-25T08:44:00","date_gmt":"2006-07-25T08:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/25\/how-can-i-delete-all-the-email-sent-from-a-specific-person\/"},"modified":"2006-07-25T08:44:00","modified_gmt":"2006-07-25T08:44:00","slug":"how-can-i-delete-all-the-email-sent-from-a-specific-person","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-all-the-email-sent-from-a-specific-person\/","title":{"rendered":"How Can I Delete All the Email Sent From a Specific Person?"},"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! How can I delete all the email sent from a specific person?<BR><BR>&#8212; VP<\/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, VP. Ah, yes, deleting email from a specific person; that\u2019s something we Scripting Guys are all too familiar with. For example, the first time the Scripting Guys ever heard of such a thing was back in the early days of Microsoft Mail, when Bill Gates supposedly had a \u201c<A href=\"http:\/\/www.webopedia.com\/TERM\/B\/bozo_filter.html\" target=\"_blank\"><B>bozo filter<\/B><\/A>\u201d installed on his machine that could automatically delete mail sent by a specified person or persons. (Of course, nowadays Outlook has also sorts of filters and rules built right into the product. But back then \u2013 or so the story goes \u2013 only Bill\u2019s version of Microsoft Mail had this capability.)<\/P>\n<P>We can\u2019t say for sure that Bill had this bozo filter constructed specifically to delete mail sent from the Scripting Guys; when you consider the fact that the Scripting Guys didn\u2019t even exist back then, well, that\u2019s a reasonably good argument against that notion. On the other hand, there <I>is<\/I> the name they gave this contraption. Can you think of anyone who better fits the definition of \u201cbozo\u201d than the Scripting Guys? With that in mind, the Scripting Guys got together and developed a script that zips through our Inboxes and deletes any mail Bill sends to <I>us<\/I>. Does this script work? Let\u2019s put it this way: you won\u2019t find mail from Bill Gates in any of our Inboxes. No way.<\/P>\n<P>In other words, if Bill Gates has been hounding you day and night with questions, business proposals, and what-have-you, well, here\u2019s a script that will delete all that mail from your Inbox:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)<\/p>\n<p>Set colItems = objFolder.Items\nSet colFilteredItems = colItems.Restrict(&#8220;[From] = &#8216;Bill Gates'&#8221;)<\/p>\n<p>For i = colFilteredItems.Count to 1 Step -1\n    colFilteredItems(i).Delete\nNext\n<\/PRE>\n<P>You know, that\u2019s a very good question: how exactly <I>does<\/I> this script work? Well, we start out by defining a constant named olFolderInbox and setting the value to 6; we\u2019ll use this constant to tell the script which Outlook folder we want to work with. Could we work with a different folder, or \u2013 even better \u2013 could we work with all the folders and subfolders in the Inbox? Sure; for an example of working with all the folders and subfolders in your Outlook Inbox take a look at this <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0616.mspx\"><B>Hey, Scripting Guy! column<\/B><\/A>.<\/P>\n<TABLE id=\"EXD\" 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>. Don\u2019t worry: none of our Web pages include a bozo filter of any kind. They can\u2019t; otherwise how would <I>we<\/I> access those pages?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After defining the constant we create an instance of the <B>Outlook.Application<\/B> object, and then use the <B>GetNamespace<\/B> method to bind to the MAPI namespace (which we need to do even though the MAPI namespace is the <I>only<\/I> namespace we can bind to). Once we\u2019re bound to the MAPI namespace we can then use this line of code to connect to the Inbox:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>After we\u2019ve made the connection to the Inbox we <I>could<\/I> methodically walk through each and every mail message, checking to see if the message came from Bill Gates and, if it did, delete it. That works, but it\u2019s a bit cumbersome. And if you\u2019ve seen some of the Inboxes around here, be forewarned that a script that took that approach could also take forever to complete.<\/P>\n<TABLE id=\"EWE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>True story<\/B>. The Scripting Guy who writes this column once had a manager who asked him to send her something by email. He did, but the manager said she couldn\u2019t find the message anywhere. She asked the Scripting Guy if he could glance through her Inbox and pick out the message for her. When he went to look for that elusive message, he discovered that her Inbox contained over 14,000 items, 13,800 of which were unread. (Including pretty much every email the Scripting Guy had ever sent her.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>With that in mind we decided to do something a little fancier, and a little more efficient. To begin with, we use this line of code to grab a collection of all the items found in the Inbox:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>Like we said, though, we aren\u2019t going to start walking through this entire collection. Instead, we\u2019re going to apply a filter of our own, one that restricts the collection to those messages sent by Bill Gates:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict(&#8220;[From] = &#8216;Bill Gates'&#8221;)\n<\/PRE>\n<P>As you can see, we simply call the <B>Restrict<\/B> method, indicating that we want to restrict the collection to items where the <B>From<\/B> property is equal to <I>Bill Gates<\/I>. Just like that, our collection will be filtered, and the only things left in colItems will be messages that were sent from Bill Gates.<\/P>\n<TABLE id=\"EQF\" 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>. Can you filter on properties other than From? You bet you can; for more information, take a look at this <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0818.mspx\"><B>Office Space column<\/B><\/A> devoted to that very subject.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Why do we use a filter? Well, for one thing, we now have a collection that contains <I>only<\/I> messages sent by Bill Gates; that means we don\u2019t have to walk through all the items in the Inbox and laboriously check each message to see who sent it. Instead, we have a collection where we already <I>know<\/I> that each message was sent from Bill Gates. In turn, that means we can set up a For Next loop to walk through our filtered collection; each time through the loop we can then call the <B>Delete<\/B> method to delete the message:<\/P><PRE class=\"codeSample\">For i = colFilteredItems.Count to 1 Step -1\n    colFilteredItems(i).Delete\nNext\n<\/PRE>\n<P>Very observant of you: that <I>is<\/I> a somewhat unusual-looking For Next loop, isn\u2019t it? With this loop we start with the very last item in the collection (which we can determine by checking the value of the <B>Count<\/B> property). We delete that item and then delete the next-to-last item in the collection, continuing in this vein until we finally delete the first item in the collection. (The <B>Step -1<\/B> parameter tells the script to count backwards as it loops through the collection.) <\/P>\n<P>Why don\u2019t we just start at the beginning and then stop at the end? As it turns out, if you start deleting items from a collection Outlook can lose its place; that\u2019s because, as items get deleted, the order of those items \u2013 and thus the nature of the collection \u2013 begins to change. For example, say you delete item 1. Obviously the next thing you\u2019ll want to do is delete item 2. However, what used to be item 2 is now item 1 (because item 1 is gone). Consequently, you\u2019ll end up deleting item <I>3<\/I> instead. See how that works? If you follow this all the way through, you\u2019ll end up deleting only about half the items. <\/P>\n<P>That\u2019s right: we wanted to delete <I>all<\/I> the items, not just every other item. Fortunately we can do that as log as we start at the bottom and work our way up. With that approach, items don\u2019t change position as other items are deleted; if you delete item 5, item 1 will still be item 1. As long as we delete items in inverse order \u2013 5-4-3-2-1 \u2013 everything will be fine.<\/P>\n<P>It\u2019s weird, but it works. Just like the Scripting Guys.<\/P>\n<P>Well, OK: except maybe for the part about working. But, hey, we must be doing <I>something<\/I> right; why else would they have named an email filter after us?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete all the email sent from a specific person?&#8212; VP Hey, VP. Ah, yes, deleting email from a specific person; that\u2019s something we Scripting Guys are all too familiar with. For example, the first time the Scripting Guys ever heard of such a thing was back in the early [&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-66833","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! How can I delete all the email sent from a specific person?&#8212; VP Hey, VP. Ah, yes, deleting email from a specific person; that\u2019s something we Scripting Guys are all too familiar with. For example, the first time the Scripting Guys ever heard of such a thing was back in the early [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66833","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=66833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66833\/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=66833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}