{"id":63863,"date":"2007-10-05T22:17:00","date_gmt":"2007-10-05T22:17:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/05\/hey-scripting-guy-how-can-i-save-the-attachments-for-all-my-new-office-outlook-messages\/"},"modified":"2007-10-05T22:17:00","modified_gmt":"2007-10-05T22:17:00","slug":"hey-scripting-guy-how-can-i-save-the-attachments-for-all-my-new-office-outlook-messages","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-save-the-attachments-for-all-my-new-office-outlook-messages\/","title":{"rendered":"Hey, Scripting Guy! How Can I Save the Attachments for All My New Office Outlook Messages?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! I\u2019ve been testing your script for <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0809.mspx\"><B>saving all the attachments in your Outlook Inbox<\/B><\/A>, and it works great. However, I have a question: is it possible to get this script to run only against new emails?<BR><BR>&#8212; CN<\/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, CN. Welcome to today\u2019s <I>Hey, Scripting Guy!<\/I> column, brought to you on-location from the La Quinta Inn in beautiful downtown Moscow, Idaho!<\/P>\n<P>What\u2019s that? Why is the Scripting Guy who writes this column at the La Quinta Inn in beautiful downtown Moscow, Idaho? Because today is <A href=\"http:\/\/www.students.uidaho.edu\/futurestudents\/vandalpreview\" target=\"_blank\"><B>Vandal Preview Day<\/B><\/A>, of course. <\/P>\n<P>Duh \u2026.<\/P>\n<P>Pardon? What\u2019s Vandal Preview Day? Good heavens, have you guys been living in a cave or something the past few years?<\/P>\n<P>Oh; sorry to hear that. Well, we\u2019re glad to hear that you\u2019re all finally <I>out<\/I> of that cave; that must have been rough. Anyway, as we were saying, Vandal Preview Day is a semi-annual event sponsored by the University of Idaho (located in beautiful downtown Moscow, Idaho). The idea is to bring prospective college freshmen and their parents in to tour the campus, eat lunch in the Wallace Food Court, and \u2013 according to the Welcome email we received \u2013 spend money on souvenirs. Like maybe a <A href=\"http:\/\/idaho.teamfanshop.com\/COLLEGE_Idaho_Vandals\/Black_And_Gold_Lei\" target=\"_blank\"><B>black-and-gold University of Idaho lei<\/B><\/A>.<\/P>\n<P>Well, at least for those people who don\u2019t already have one.<\/P>\n<P>So why did the Scripting Family drive some 300 miles to attend Vandal Preview Day? (OK, fine; according to the Internet they drove 297.17 miles.) That\u2019s a question we can answer: because the Scripting Son is seriously thinking about attending the University of Idaho next year. <I>Why<\/I> is the Scripting Son seriously thinking about attending the University of Idaho next year? That\u2019s a tougher one; we\u2019ll have to get back to you on that one.<\/P>\n<TABLE id=\"EAE\" 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>. Is it maybe because the University of Idaho\u2019s \u201cdistinctive academic programs offer you unparalleled opportunities to deepen your knowledge and explore ways to better the world\u201d? No, it\u2019s mainly because two of the Scripting Cousins go there.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, the Scripting Family is about to go eat their free continental breakfast, and then head out to the campus. Before we go, however, we\u2019d like to take a moment to show you a script that can save all the attachments in your Outlook Inbox, but only for new emails.<\/P>\n<P>After all, no trip to beautiful downtown Moscow, Idaho would be complete if we <I>didn\u2019t<\/I> show you such a script.<\/P>\n<P>Here you go:<\/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] = &#8216;True'&#8221;)<\/p>\n<p>For Each objMessage In colFilteredItems\n    intCount = objMessage.Attachments.Count\n    If intCount &gt; 0 Then\n        For i = 1 To intCount\n            objMessage.Attachments.Item(i).SaveAsFile &#8220;C:\\Temp\\&#8221; &amp;  _\n                objMessage.Attachments.Item(i).FileName\n        Next\n    End If\nNext\n<\/PRE>\n<P>The biggest problem we ran into when was sat down to write this script was deciding what qualified as a new email and what didn\u2019t. For this first script, we decided a new email was simply an email that hadn\u2019t been read yet. Of course, a good case can be made that a new email is any mail which was received after a specified date. Do you like that definition of \u201cnew email\u201d better? Then we have good news for you: in just a second we\u2019ll show you a script that works solely with emails received after a specified date.<\/P>\n<P>For now, however, let\u2019s talk about the script that saves the attachments for all your unread messages (regardless of the date received). To carry out this task, we first define a constant named olFolderInbox and set the value to 6; we\u2019ll use that constant to tell Outlook which mailbox folder we want to work with. We then use the following three lines of code to create an instance of the <B>Outlook.Application<\/B> object, bind to the <B>MAPI<\/B> namespace, and then create an object reference to the Inbox folder:<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>After we\u2019ve made a connection to the Inbox folder we use this line of code to retrieve a collection of all the emails found in the Inbox:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>Ah, good point: we don\u2019t want <I>all<\/I> the emails found in the Inbox, do we? Instead, we want only the <I>unread<\/I> messages. Well, don\u2019t worry; that\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict(&#8220;[UnRead] = &#8216;True'&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is using the <B>Restrict<\/B> method to apply a filter to our collection of emails. In this case, we\u2019re restricting returned data to those messages where the <B>UnRead<\/B> property is True; as you might expect, if the UnRead property is True that means that the email has not been read yet.<\/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><B>Note<\/B>. Why is the property name enclosed in square brackets? Good question and, to be honest, we don\u2019t know the answer, other than to say, \u201cThat\u2019s just the way you do it.\u201d<\/P>\n<P>Or at least the way we do it here in Idaho.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As soon as we have our filtered collection (which we named colFilteredItems) we set up a For Each loop to loop through all the emails in that collection. Inside that loop, we kick things off by retrieving the number of attachments (if any) that belong to the first message in the collection:<\/P><PRE class=\"codeSample\">intCount = objMessage.Attachments.Count\n<\/PRE>\n<P>If the value of the <B>Count<\/B> property is 0 that means that the message <I>has<\/I> no attachments. If the Count is greater than 0 that means that the message has at least one attachment. With that in mind, we next set up a For Next loop that runs from 1 to the total number of attachments for that message. (As you know, a given email can have more than one attachment.) For each attachment we then call the <B>SaveAsFile<\/B> method, saving the file (using the attachment\u2019s <B>FileName<\/B>) in the folder C:\\Temp<\/P><PRE class=\"codeSample\">objMessage.Attachments.Item(i).SaveAsFile &#8220;C:\\Temp\\&#8221; &amp;  _\n    objMessage.Attachments.Item(i).FileName\n<\/PRE>\n<P>After we\u2019ve saved all the attachments for the first message we then loop around and repeat the process with the next message in the collection.<\/P>\n<TABLE id=\"EOG\" 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>. By the way, CN, we know you had a second question which involves renaming the files as you save them. That\u2019s a question we\u2019ll have to address some other time.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, what about a script that saves the attachments for all the emails received after a specified date? Well, here\u2019s a sample script that restricts returned messages to those received after midnight, October 1, 2007:<\/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;[ReceivedTime] &gt; &#8217;10\/1\/2007 12:00 AM'&#8221;)<\/p>\n<p>For Each objMessage In colFilteredItems\n    intCount = objMessage.Attachments.Count\n    If intCount &gt; 0 Then\n        For i = 1 To intCount\n            objMessage.Attachments.Item(i).SaveAsFile &#8220;C:\\Temp\\&#8221; &amp;  _\n                objMessage.Attachments.Item(i).FileName\n        Next\n    End If\nNext\n<\/PRE>\n<P>If this looks vaguely familiar, well, it should: the only difference between this script and the first one we showed you is the filter we applied:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict(&#8220;[ReceivedTime] &gt; &#8217;10\/1\/2007 12:00 AM'&#8221;)\n<\/PRE>\n<P>As you can see, what we\u2019re doing here is restricting returned messages to those where the <B>ReceivedTime<\/B> property (the date and time the message was received) is greater than 10\/1\/2007 12:00 AM. Could we apply a filter that restricts our messages to those received during a specific time period? You bet; here\u2019s a filter that weeds out everything except messages received on October 1, 2007 or October 2, 2007:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict _\n    (&#8220;[ReceivedTime] &gt; &#8217;10\/1\/2007 12:00 AM&#8217; AND [ReceivedTime] &lt; &#8217;10\/3\/2007 12:00 AM'&#8221;)\n<\/PRE>\n<P>And yes, we <I>could<\/I> do this all day long. Except we can\u2019t; it\u2019s time for Vandal Preview Day and the most important part of the event: a talk entitled <B>What Can Parents do to <\/B><B>Finance<\/B><B><\/B><B>College<\/B><B>?<\/B><\/P>\n<P>And no, dying and allowing the Scripting Son to collect the life insurance is <I>not<\/I> an option.<\/P>\n<P>At least not at the moment.<\/P>\n<P>In case you\u2019re wondering, the Scripting Guy who writes this column only knows two things about the University of Idaho. First, and according to the Scripting Brother (and father of the two Scripting Cousins), the Vandals\u2019 marching band has \u201cthe funniest tuba players in the world.\u201d Second, the Vandals\u2019 favorite basketball cheer goes like this:<\/P>\n<P>I-D-A-H-O Idaho, Idaho, go, go, go!<\/P>\n<P>Now that we think about it, there really was no point in coming to Vandal Preview Day. After all, the school already has funny tuba players and a catchy little basketball cheer; what more does it need?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019ve been testing your script for saving all the attachments in your Outlook Inbox, and it works great. However, I have a question: is it possible to get this script to run only against new emails?&#8212; CN Hey, CN. Welcome to today\u2019s Hey, Scripting Guy! column, brought to you on-location from 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-63863","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! I\u2019ve been testing your script for saving all the attachments in your Outlook Inbox, and it works great. However, I have a question: is it possible to get this script to run only against new emails?&#8212; CN Hey, CN. Welcome to today\u2019s Hey, Scripting Guy! column, brought to you on-location from the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63863","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=63863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63863\/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=63863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}