{"id":64263,"date":"2007-08-10T01:22:00","date_gmt":"2007-08-10T01:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/10\/how-can-i-save-emails-that-are-more-than-one-month-old\/"},"modified":"2007-08-10T01:22:00","modified_gmt":"2007-08-10T01:22:00","slug":"how-can-i-save-emails-that-are-more-than-one-month-old","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-emails-that-are-more-than-one-month-old\/","title":{"rendered":"How Can I Save Emails That Are More Than One Month 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! How can I save all the items in my Sent Items folder that are more than one month old?<BR><BR>&#8212; RS<\/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, RS. You know, we\u2019d like to answer this question; we really would. However, the Scripting Guy who writes this column is leaving on vacation tomorrow, which means that \u2013 being an important and valued employee of the Microsoft Corporation \u2013 he has an awful lot to do before he can take off. For example, he has to \u2013 well, never mind; they don\u2019t let him do that anymore. But he <I>does<\/I> have to arrange for \u2013 no, scratch that; although the fire wasn\u2019t <I>really<\/I> his fault, they don\u2019t let him do that anymore, either. Of course, he still has to \u2013 you know what? Maybe, despite his busy schedule, he can answer this question for you after all:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const olFolderSentMail = 5\nConst olMSG = 3<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)<\/p>\n<p>dtmTargetDate = Date &#8211; 30<\/p>\n<p>Set colItems = objFolder.Items\nSet colFilteredItems = colItems.Restrict(&#8220;[CreationTime] &lt;&#8216;&#8221; &amp; dtmTargetDate &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objMessage In colFilteredItems\n    strName = objMessage.Subject\n    strName = Replace(strName, &#8220;:&#8221;, &#8220;&#8221;)\n    strName = Replace(strName,&#8221;\/&#8221;,&#8221;&#8221;)\n    strName = Replace(strName,&#8221;\\&#8221;,&#8221;&#8221;)\n    strName = Replace(strName,&#8221;,&#8221;,&#8221;&#8221;)\n    strName = Replace(strName, Chr(34),&#8221;&#8221;)\n    strName = Replace(strName,Chr(39),&#8221;&#8221;)\n    strName = Replace(strName,&#8221;?&#8221;,&#8221;&#8221;)<\/p>\n<p>    strName = &#8220;C:\\Test\\&#8221; &amp; strName &amp; &#8220;.msg&#8221;\n    objMessage.SaveAs strName, olMSG  \nNext\n<\/PRE>\n<P>Let\u2019s see if we can figure out how this script works. (Incidentally, the script isn\u2019t anywhere near as complicated as it looks. And don\u2019t worry: we\u2019ll explain <I>why<\/I> it looks so complicated in a minute or two.) <\/P>\n<P>As you can see, we start out by defining a pair of constants: olFolderSentMail (which represents the mail folder we want to access, the Sent Items folder), and olMSG (which represents the file format \u2013 Outlook message format \u2013 we want to use when saving each item in the folder). After that we create an instance of the <B>Outlook.Application<\/B> object, bind to the MAPI namespace, then use this line of code to connect to the Sent Items folder:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)\n<\/PRE>\n<P>Whew!<\/P>\n<P>Our end goal is to go through the Sent Items folder and save a copy of each message that\u2019s more than a month old. (Or, for our purposes, more than 30 days old.) How are we supposed to <I>know<\/I> whether or not a message is more than a month old? Actually, that\u2019s pretty easy to determine: all we have to do is compare the message\u2019s <B>CreationTime<\/B> property with the date from a month ago. If the CreationTime is less than that date, then the message must be more than one month old.<\/P>\n<P>Of course, in order to do that we need to know exactly what <I>was<\/I> the date 30 days prior to today. Fortunately, we can determine <I>that<\/I> simply by subtracting 30 days from the current date, something we do here:<\/P><PRE class=\"codeSample\">dtmTargetDate = Date &#8211; 30\n<\/PRE>\n<P>Now we\u2019re ready to retrieve a collection of all the sent mail items that are more than 30 days old. The first step in that process is to use this line of code to retrieve a collection of <I>all<\/I> the items in the Sent Items folder:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>From there we apply a <B>Filter<\/B> to the collection, restricting the data to items where the CreationTime property is earlier (less than) the value of the variable dtmTargetDate:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict(&#8220;[CreationTime] &lt;&#8216;&#8221; &amp; dtmTargetDate &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>And no, we won\u2019t spend any time talking about Outlook filters in today\u2019s column; for more information, see our article <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0818.mspx\"><B>Filtering Email Messages in Microsoft Outlook<\/B><\/A>. The main things to note for now: you need to put square brackets around the property name (<B>[CreationTime]<\/B>) and, even though dtmTargetDate is a date-time value, you need to treat it like a string value; hence the crazy quotation marks:<\/P><PRE class=\"codeSample\">&#8220;[CreationTime] &lt;&#8216;&#8221; &amp; dtmTargetDate &amp; &#8220;&#8216;&#8221;\n<\/PRE>\n<P>At this point we\u2019d be done (pretty much), except for one thing. We\u2019ve decided to use the value of the <B>Subject<\/B> property as the file name for each saved file. That\u2019s fine, except that email Subject lines often include characters (such as colons) that can\u2019t be included in file names:<\/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>RE: The TechNet Script Center<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Because of that, we can\u2019t save a message until we remove these illegal characters. How are we going to do that? We\u2019re glad you asked that question.<\/P>\n<P>To begin with, we set up a For Each loop to loop through all the items in our collection; inside that loop, we assign the value of the Subject property to a variable named strName. That\u2019s what these two lines of code are for:<\/P><PRE class=\"codeSample\">For Each objMessage In colFilteredItems\n    strName = objMessage.Subject\n<\/PRE>\n<P>We then use a series of <B>Replace<\/B> commands to replace invalid file name characters with, for our purposes, nothing. For example, this command removes any colons from the variable strName:<\/P><PRE class=\"codeSample\">strName = Replace(strName, &#8220;:&#8221;, &#8220;&#8221;)\n<\/PRE>\n<P>If you take a closer look at the For Each loop, you\u2019ll see that we have similar commands to remove periods, commas, single quote marks (<B>chr(39)<\/B>) and double quote marks (<B>chr(34)<\/B>). Of course, there might be other characters you need to remove in order to create valid file names; if that\u2019s the case, well, we\u2019ve left it up to you to add in additional Replace commands as needed.<\/P>\n<P>After we\u2019ve cleaned up the value of the variable strName we then use this line of code to construct a file path for our new file:<\/P><PRE class=\"codeSample\">strName = &#8220;C:\\Test\\&#8221; &amp; strName &amp; &#8220;.msg&#8221;\n<\/PRE>\n<P>This gives us a file path similar to the following (note that the colon following the <I>RE<\/I> has been removed):<\/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>C:\\Test\\RE The TechNet Script Center.msg<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>From there we can save the file by calling the <B>SaveAs<\/B> method, passing the file path and the file type (the constant olMSG) as the two method parameters:<\/P><PRE class=\"codeSample\">objMessage.SaveAs strName, olMSG\n<\/PRE>\n<P>And then it\u2019s back to the top of the loop, where we repeat the process with the next item in the collection.<\/P>\n<P>That should get you going, RS. And now, all kidding aside, it\u2019s time for the Scripting Guy who writes this column to do what he <I>really<\/I> does each and every day: go to lunch with the other Scripting Guys. Sure, it\u2019s a tough job. But someone has to do it.<\/P>\n<TABLE class=\"dataTable\" id=\"EJG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Incidentally, that\u2019s not just a figure of speech: someone <I>does<\/I> have to do it. Or at least someone has to go with Scripting Guy Dean Tsaltas, who inevitably forgets either his card key or the way to the cafeteria.<\/P>\n<P>Or both.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I save all the items in my Sent Items folder that are more than one month old?&#8212; RS Hey, RS. You know, we\u2019d like to answer this question; we really would. However, the Scripting Guy who writes this column is leaving on vacation tomorrow, which means that \u2013 being an [&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-64263","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 save all the items in my Sent Items folder that are more than one month old?&#8212; RS Hey, RS. You know, we\u2019d like to answer this question; we really would. However, the Scripting Guy who writes this column is leaving on vacation tomorrow, which means that \u2013 being an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64263","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=64263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64263\/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=64263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}