Hey, Scripting Guy! How Can I Save the Attachments for All My New Office Outlook Messages?

Hey, Scripting Guy! How Can I Save the Attachments for All My New Office Outlook Messages?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’ve 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?

-- CN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CN. Welcome to today’s Hey, Scripting Guy! column, brought to you on-location from the La Quinta Inn in beautiful downtown Moscow, Idaho!

What’s that? Why is the Scripting Guy who writes this column at the La Quinta Inn in beautiful downtown Moscow, Idaho? Because today is Vandal Preview Day, of course.

Duh ….

Pardon? What’s Vandal Preview Day? Good heavens, have you guys been living in a cave or something the past few years?

Oh; sorry to hear that. Well, we’re glad to hear that you’re all finally out 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 – according to the Welcome email we received – spend money on souvenirs. Like maybe a black-and-gold University of Idaho lei.

Well, at least for those people who don’t already have one.

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’s a question we can answer: because the Scripting Son is seriously thinking about attending the University of Idaho next year. Why is the Scripting Son seriously thinking about attending the University of Idaho next year? That’s a tougher one; we’ll have to get back to you on that one.

Note. Is it maybe because the University of Idaho’s “distinctive academic programs offer you unparalleled opportunities to deepen your knowledge and explore ways to better the world”? No, it’s mainly because two of the Scripting Cousins go there.

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’d 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.

After all, no trip to beautiful downtown Moscow, Idaho would be complete if we didn’t show you such a script.

Here you go:

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[UnRead] = 'True'")

For Each objMessage In colFilteredItems
    intCount = objMessage.Attachments.Count
    If intCount > 0 Then
        For i = 1 To intCount
            objMessage.Attachments.Item(i).SaveAsFile "C:\Temp\" &  _
                objMessage.Attachments.Item(i).FileName
        Next
    End If
Next

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’t. For this first script, we decided a new email was simply an email that hadn’t 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 “new email” better? Then we have good news for you: in just a second we’ll show you a script that works solely with emails received after a specified date.

For now, however, let’s 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’ll 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 Outlook.Application object, bind to the MAPI namespace, and then create an object reference to the Inbox folder:

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

After we’ve 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:

Set colItems = objFolder.Items

Ah, good point: we don’t want all the emails found in the Inbox, do we? Instead, we want only the unread messages. Well, don’t worry; that’s what this line of code is for:

Set colFilteredItems = colItems.Restrict("[UnRead] = 'True'")

What we’re doing here is using the Restrict method to apply a filter to our collection of emails. In this case, we’re restricting returned data to those messages where the UnRead property is True; as you might expect, if the UnRead property is True that means that the email has not been read yet.

Note. Why is the property name enclosed in square brackets? Good question and, to be honest, we don’t know the answer, other than to say, “That’s just the way you do it.”

Or at least the way we do it here in Idaho.

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:

intCount = objMessage.Attachments.Count

If the value of the Count property is 0 that means that the message has 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 SaveAsFile method, saving the file (using the attachment’s FileName) in the folder C:\Temp

objMessage.Attachments.Item(i).SaveAsFile "C:\Temp\" &  _
    objMessage.Attachments.Item(i).FileName

After we’ve saved all the attachments for the first message we then loop around and repeat the process with the next message in the collection.

Note. By the way, CN, we know you had a second question which involves renaming the files as you save them. That’s a question we’ll have to address some other time.

Now, what about a script that saves the attachments for all the emails received after a specified date? Well, here’s a sample script that restricts returned messages to those received after midnight, October 1, 2007:

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[ReceivedTime] > '10/1/2007 12:00 AM'")

For Each objMessage In colFilteredItems
    intCount = objMessage.Attachments.Count
    If intCount > 0 Then
        For i = 1 To intCount
            objMessage.Attachments.Item(i).SaveAsFile "C:\Temp\" &  _
                objMessage.Attachments.Item(i).FileName
        Next
    End If
Next

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:

Set colFilteredItems = colItems.Restrict("[ReceivedTime] > '10/1/2007 12:00 AM'")

As you can see, what we’re doing here is restricting returned messages to those where the ReceivedTime 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’s a filter that weeds out everything except messages received on October 1, 2007 or October 2, 2007:

Set colFilteredItems = colItems.Restrict _
    ("[ReceivedTime] > '10/1/2007 12:00 AM' AND [ReceivedTime] < '10/3/2007 12:00 AM'")

And yes, we could do this all day long. Except we can’t; it’s time for Vandal Preview Day and the most important part of the event: a talk entitled What Can Parents do to FinanceCollege?

And no, dying and allowing the Scripting Son to collect the life insurance is not an option.

At least not at the moment.

In case you’re 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’ marching band has “the funniest tuba players in the world.” Second, the Vandals’ favorite basketball cheer goes like this:

I-D-A-H-O Idaho, Idaho, go, go, go!

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?

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hey Scripting Guy, this is really great and much appreciated. Is there a way to filter the collection of emails to only "today" AND match some string in the Subject line?

  • Is there a way to do this for public folders?   (I'm sure there is a way...)