Save Incoming Attachments, Choose Your Folder


May 23, 2008 – 1:46 am by JP

Here's a sub and a function for Outlook that let's you save attachments from a selected or open email. It's similar to the File>Save Attachments menu option, which lets you save all of the attachments from a particular email, or the FindControl Method, which can be used to find and execute "Save Attachments".

Now I realize that there is already a ton of code available that you can use to save attachments, but I found some code that uses the Shell application to let you pick the folder you want to use as the destination. So my purpose here is to demonstrate how to use this code in a simple routine.

It's a great technique and, since it is a separate function, it is very portable to other subs where you want to let the user pick a folder on their hard drive or network drive as the destination for files you want to save or use in a routine. It simply returns the full path as a string.

As you can see below, the String variable "SelectedFolder" is used to store the returned results from the SelectFolder() Function.

As usual, we set an object reference to a Mail Item, then loop through the attachments collection of that mail item and save each one. Notice that the bulk of the work is done inside the If statement (but outside the loop) to avoid costly object references that might be useless if the email doesn't have attachments (for example, if you run this code on the wrong email by mistake).

VBA:
  1. Sub GoThroughAttachments()
  2. Dim MyItem As Outlook.MailItem
  3. Dim myAttachments As Outlook.Attachments
  4. Dim i As Long
  5. Dim Att As String
  6. Dim SelectedFolder As String
  7.  
  8. On Error Resume Next
  9. Select Case TypeName(Application.ActiveWindow)
  10.     Case "Explorer"
  11.         Set MyItem = ActiveExplorer.Selection.Item(1)
  12.     Case "Inspector"
  13.         Set MyItem = ActiveInspector.CurrentItem
  14.     Case Else
  15. End Select
  16. On Error GoTo 0
  17.  
  18. If MyItem Is Nothing Then
  19.     GoTo ExitProc
  20. End If
  21.  
  22. If MyItem.Attachments.Count> 0 Then
  23.     SelectedFolder = SelectFolder()
  24.  
  25.     If SelectedFolder <> "" Then
  26.      ' user didn’t press Cancel
  27.  
  28.     Set myAttachments = MyItem.Attachments
  29.  
  30.     For i = 1 To myAttachments.Count
  31.         Att = myAttachments.Item(i).DisplayName
  32.         myAttachments.Item(i).SaveAsFile SelectedFolder & "\" & Att
  33.     Next i
  34.     End If
  35. End If
  36.  
  37. ExitProc:
  38. Set myAttachments = Nothing
  39. Set MyItem = Nothing
  40. End Sub

VBA:
  1. Private Function SelectFolder(Optional i_RootFolder As String) As String
  2. ' from http://vba-corner.livejournal.com/
  3. Dim myShell As Object
  4. Dim MyFolder As Object
  5.  
  6.   Set myShell = CreateObject(Shell.Application)
  7.   If i_RootFolder = "" Then
  8.     'no root folder given, use default (which is Desktop)
  9.     Set MyFolder = myShell.BrowseForFolder(0, "Please select a folder:", 1)
  10.   ElseIf Not (i_RootFolder Like "*[!0123456789]*") Then
  11.     'number for special folder given
  12.     Set MyFolder = myShell.BrowseForFolder(0, "Please select a folder:", 1, CInt(i_RootFolder))
  13.   Else
  14.     'path for root folder given
  15.     Set MyFolder = myShell.BrowseForFolder(0, "Please select a folder:", 1, CStr(i_RootFolder))
  16.   End If
  17.   If Not MyFolder Is Nothing Then
  18.     SelectFolder = MyFolder.self.Path
  19.   End If
  20. End Function

Notice that the SelectFolder() Function does not include the trailing slash at the end, so we need to add this when saving the file.

If you wanted to simply use the same folder every time (for example, C:\MyFiles\), just comment out this line:

VBA:
  1. SelectedFolder = SelectFolder()

Remove the inner If-End-If statement (If SelectedFolder "" Then) and change this line:

VBA:
  1. myAttachments.Item(i).SaveAsFile SelectedFolder & "\" & Att

to this:

VBA:
  1. myAttachments.Item(i).SaveAsFile "C:\MyFiles\" & Att

But why would you want to do a thing like that, when the Shell automation is so much cooler?

Click here to see how to add this code to a toolbar button in Outlook.

Enjoy,
JP


If you enjoyed this page:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Outlook, VBA, automation
Tags: , , ,

This post has 250 views since May 23, 2008 – 1:46 am.

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Read those image files in Outlook || XML Parsing Function »