{"id":64543,"date":"2007-06-30T00:24:00","date_gmt":"2007-06-30T00:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/30\/how-can-i-delete-unread-emails-that-are-more-than-6-months-old\/"},"modified":"2007-06-30T00:24:00","modified_gmt":"2007-06-30T00:24:00","slug":"how-can-i-delete-unread-emails-that-are-more-than-6-months-old","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-unread-emails-that-are-more-than-6-months-old\/","title":{"rendered":"How Can I Delete Unread Emails That Are More Than 6 Months Old?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! We have a lot of users who let unread email messages pile up in their Outlook Inboxes; after awhile, that causes their Inboxes to fill up. How can I write a script users could run that would delete any unread messages that are more than 6 months old?<BR><BR>&#8212; ST <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG 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 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> \n<P>Hey, ST. You know, sometimes it\u2019s tough being a Scripting Guy. After all, people don\u2019t come to the Script Center just hoping to pick up a tip or two about system administration scripting; instead, people come to the Script Center hoping to be uplifted and inspired. Even well-known TV personalities like <A href=\"http:\/\/www.oprah.com\/\" target=\"_blank\"><B>Oprah Winfrey<\/B><\/A> and <A href=\"http:\/\/www.drphil.com\/\" target=\"_blank\"><B>Dr. Phil<\/B><\/A> don\u2019t bother trying to help people anymore; instead, they simply say, \u201cListen, you want to be uplifted and inspired? Then read the <I>Hey, Scripting Guy!<\/I> column every morning.\u201d<\/P>\n<TABLE class=\"dataTable\" id=\"ELD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Disclaimer<\/B>. OK, so, technically, neither Oprah Winfrey nor Dr. Phil has ever said anything even <I>remotely<\/I> similar to that. However, if you watch their shows, and if you carefully observe their body language, well, it\u2019s pretty obvious what they\u2019re implying.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, as a general rule, the Scripting Guys welcome the responsibility of spreading joy and happiness throughout the world. However, fulfilling their roles as the Happiness Fairies can be difficult at times, especially during a week like this one. What\u2019s so bad about <I>this<\/I> week? Boy, where do we start? For one thing, the Scripting Guy who writes this column started the week off with a car that was broken and could <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0626.mspx\"><B>never be fixed<\/B><\/A>; saw that change to a car that could be fixed simply by <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0627.mspx\"><B>replacing a $1.25 part<\/B><\/A>; then ended the week with a car that was broken and could <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0628.mspx\"><B>never be fixed<\/B><\/A>. <\/P>\n<P>Meanwhile, Scripting Guy Jean Ross made arrangements to have a few things taken care of, the only stipulation being that everything had to be taken care of by July 3<SUP>rd<\/SUP>. No problem, she was told; everything will be done by July 3<SUP>rd<\/SUP>. When she went in to sign the final contract they told her, \u201cHere you go, ma\u2019am. Everything will be done by July 13<SUP>th<\/SUP>, just like you asked.\u201d<\/P>\n<TABLE class=\"dataTable\" id=\"E2E\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. The punch line? On top of everything else, the stuff they told her would be done by July 13<SUP>th<\/SUP> wasn\u2019t even the stuff she had asked them to do in the first place.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s see, what else \u2026. Well, TechNet is planning to make some changes to the publishing process, changes that will make the Scripting Guys\u2019 work life much \u2026 better \u2026. (Apparently TechNet didn\u2019t think it was challenging enough for a two-person team to write, publish, and maintain the entire Script Center. Therefore they decided to up the ante a little.) As for the icing on the cake, we\u2019re also in the midst of performance reviews here at Microsoft. We\u2019re probably not supposed to share this information, but here\u2019s an excerpt from last year\u2019s performance review for the Scripting Guy who writes this column:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Achievements and Contributions<\/B>: None<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Skills and Competencies<\/B>: None<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Expectations for Future Success<\/B>: None<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Too bad for him that they don\u2019t have a category for consistency. <I>That<\/I> he has.<\/P>\n<P>But hey, you didn\u2019t come to the Script Center to listen to the Scripting Guys bemoan their fates, did you? Instead, you came here because Oprah Winfrey and Dr. Phil promised that the Scripting Guys would uplift and inspire you. OK, let\u2019s see what we can do about that \u2026 uplifting and inspiring \u2026 hmmm \u2026 <\/P>\n<P>Oh, we know: Suppose we show you a script that can delete all the unread messages in your Outlook Inbox that are more than 6 months old? You know, a script like this one:<\/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;[UnRead] = True&#8221;)<\/p>\n<p>For i = colFilteredItems.Count to 1 Step &#8211; 1\n    If DateDiff(&#8220;m&#8221;, colFilteredItems(i).ReceivedTime, Now) &gt; 6 Then\n        colFilteredItems(i).Delete\n    End If\nNext\n<\/PRE>\n<P>Feel better? Good; after all, your happiness is the only thing that matters to the Scripting Guys. Of course, you\u2019d probably feel even <I>better<\/I> if you understood how this script works. Very well; let\u2019s see if the two Happiness Fairies can help you with that.<\/P>\n<P>To begin with, we Happiness Fairies start out by defining a constant named olFolderInbox, then assign olFolderInbox the value 6; we\u2019ll use this constant to tell the script which Outlook folder to work with. After defining the constant we create an instance of the <B>Outlook.Application<\/B> object, then use the <B>GetNamespace<\/B> method to connect to the MAPI namespace. (A required step, even though the MAPI namespace is the <I>only<\/I> namespace we can connect to.) Finally, we use the <B>GetDefaultFolder<\/B> method to bind to the Outlook Inbox:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>Now we\u2019re ready to roll. With the connection complete we use the following line of code to retrieve a collection of all the items (that is, all the email messages) found in the Inbox folder:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>That\u2019s a good point: we\u2019re really only interested in <I>unread<\/I> emails, aren\u2019t we? That\u2019s fine; all we have to do is apply a filter and create a \u201csub-collection\u201d (named colFilteredItems) that contains only the unread emails found in the Inbox folder:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict(&#8220;[UnRead] = True&#8221;)\n<\/PRE>\n<P>We\u2019re not going to discuss email filtering in any detail today; if you\u2019d like to know more about filtering email take a look at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0818.mspx\"><B>Office Space article<\/B><\/A> on that very subject. About all we\u2019ll say here is that we\u2019re limiting our collection to items where the <B>Unread<\/B> property is True; as you might expect, if the Unread property is True that means that the message hasn\u2019t been read yet.<\/P>\n<P>Our next step is to loop through the entire collection of unread messages and then delete any emails more than 6 months old. In theory, we could have expanded our filter so that we limited the sub-collection to messages that were both unread <I>and<\/I> more than 6 months old. We didn\u2019t bother with that simply because we were afraid the filter would become unduly complicated. Instead, we\u2019re going to loop through <I>all<\/I> the unread messages, checking each one to see if it\u2019s more than 6 months old. If it is, we\u2019ll delete it. If it\u2019s not, we won\u2019t.<\/P>\n<P>Just exactly the way Dr. Phil would do things.<\/P>\n<P>Because we\u2019re going to be deleting items we need to start our loop at the bottom (that is, with the very last email in the collection) and then work our way to the top (the very first email in the collection). That\u2019s why we have a For Next loop that starts with the last item (whose index number can be determined by using the <B>Count<\/B> property) and works its way towards the first item, the one with the index number 1:<\/P><PRE class=\"codeSample\">For i = colFilteredItems.Count to 1 Step -1\n<\/PRE>\n<P>What\u2019s that? You say you have <I>two<\/I> questions? Look, we don\u2019t really feel like answering a bunch of \u2013 no, sorry. After all, we <I>are<\/I> the Happiness Fairies; we have a job to do. With that in mind, we\u2019d be <I>thrilled<\/I> to answer your questions. As to the first question (\u201cWhy the <B>Step -1<\/B> parameter?\u201d), well, that\u2019s easy: Step -1 is the key to doing a \u201cbackwards\u201d loop. Let\u2019s say we have 100 items in our collection. In that case, the counter variable i starts out equal to 100, and the first time through the loop we\u2019ll be working with item 100. When we\u2019re done with that item we need to turn our attention to item 99. How do we get from item 100 to item 99? That\u2019s right: by subtracting 1 from the current value of our counter variable. <I>That\u2019s<\/I> what Step -1 is for.<\/P>\n<P>As for your other question (\u201cWhy does deleting items require us to start at the bottom and work our way up?\u201d), well, take a peek at this <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0622.mspx\"><B>Hey, Scripting Guy! column<\/B><\/A> for an explanation.<\/P>\n<P>As we noted, inside the loop we need to take a look at each email and determine whether the message is more than 6 months old. That\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">If DateDiff(&#8220;m&#8221;, colFilteredItems(i).ReceivedTime, Now) &gt; 6 Then\n<\/PRE>\n<P>So what are we actually <I>doing<\/I> with that line of code? Well, we\u2019re using VBScript\u2019s <B>DateDiff<\/B> function to determine the number of months (the <B>\u201cm\u201d<\/B> parameter) between the date and time the message was received (<B>colFilteredItems(i).ReceivedTime<\/B>) and the current date and time (<B>Now<\/B>). If the number of months is greater than 6 we then use this line of code to delete the message:<\/P><PRE class=\"codeSample\">colFilteredItems(i).Delete\n<\/PRE>\n<P>And if the number of months is <I>not<\/I> greater than 6 then we don\u2019t do anything at all.<\/P>\n<P>Which, as our performances reviews suggest, is what we do best.<\/P>\n<P>From there we loop around and repeat the process with the next email in the collection. By the time we exit the loop we\u2019ll have deleted all the unread emails that are more than 6 months old.<\/P>\n<P>At any rate, we hope that you found today\u2019s column uplifting and inspiring, ST. If not, here\u2019s something that might help. Remember, no matter how bleak things might look, they could be worse: you could be a Scripting Guy.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have a lot of users who let unread email messages pile up in their Outlook Inboxes; after awhile, that causes their Inboxes to fill up. How can I write a script users could run that would delete any unread messages that are more than 6 months old?&#8212; ST Hey, ST. You [&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-64543","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! We have a lot of users who let unread email messages pile up in their Outlook Inboxes; after awhile, that causes their Inboxes to fill up. How can I write a script users could run that would delete any unread messages that are more than 6 months old?&#8212; ST Hey, ST. You [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64543","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=64543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64543\/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=64543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}