Microsoft Excel
Back to jkp-ads.com

Ron de Bruin
Excel Automation

Microsoft MVP Program

Save E-mail attachments to folder

Important message to visitors of this page

Ron de Bruin decided to remove all Windows Excel content from his website for personal reasons. If you want to know why, head over to rondebruin.nl.

Luckily, Ron was kind enough to allow me to publish all of his Excel content here.

Most of these pages are slightly outdated and may contain links that don 't work. Please inform me if you find such an error and I'll try to fix it.

Kind regards

Jan Karel Pieterse


Important read this :

The code on this page is only working when you use Outlook as your mail program.
Copy the code in a Standard module of your workbook, if you just started with VBA see this page.
Where do I paste the code that I find on the internet

 

Read this good

If you receive a lot of mail with attachments and you want to save the files in a folder on your computer then you can use the code on this page to save the files in the folder you want.

First right click on the Inbox and choose New Folder, in the code example I use the name "MyFolder". Note: look good the folder MyFolder must be inside your Inbox folder

Tip: Create a mail rule in Outlook (Tools>Rules and Alerts) and move the mail from ? or with the
subject ? to the folder in your Inbox named "MyFolder" when the mail arrived.
Note: You can also move the files from your Inbox to the folder "MyFolder" manual.

To test the code copy a few mails into the new folder named MyFolder inside your Inbox folder.

 

Macro example

We use two macros in this example but we only run the macro named Test with one code line.
There are the three arguments in the macro call in the Test macro :

Arg 1 = Folder name of folder inside your Inbox
Arg 2 = File extension, "" is every file
Arg 3 = Save folder, "C:\Users\Ron\test" or ""
              If you use "" it will create a date/time stamped folder for you in your "Documents" folder
              Note: If you use this "C:\Users\Ron\test" the folder must exist.


This will copy all files from “MyFolder” to a new folder in the Documents folder
    SaveEmailAttachmentsToFolder "MyFolder", "", ""

This will copy all xls files from “MyFolder” to "C:\Users\Ron\test"
    SaveEmailAttachmentsToFolder "MyFolder", "xls", "C:\Users\Ron\test"

This will copy all xlsx files from “MyFolder” to "C:\Users\Ron\test"
    SaveEmailAttachmentsToFolder "MyFolder", "xlsx", "C:\Users\Ron\test"


Set a reference to Outlook and copy/paste the code in a standard module

1) Go to the VBA editor, Alt -F11
2) Tools>References in the Menu bar
3) Place a Checkmark before Microsoft Outlook ? Object Library
    ? is the Outlook version number
4) Insert>Module
5) Paste the code (two macros) in this module
6) Alt q to close the editor
7) Save the file

Sub Test()
'Arg 1 = Folder name of folder inside your Inbox
'Arg 2 = File extension, "" is every file
'Arg 3 = Save folder, "C:\Users\Ron\test" or ""
'        If you use "" it will create a date/time stamped folder for you in your "Documents" folder
'        Note: If you use this "C:\Users\Ron\test" the folder must exist.

    SaveEmailAttachmentsToFolder "MyFolder", "xls", ""
    
End Sub

Note: You not have to change the code in the macro below. But you can change Item.SenderName to ReceivedTime in the save line like Format(Item.ReceivedTime, "yyyy-mmm-dd")

When you do that it will put the ReceivedTime before each file name instead of the SenderName

Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As String, _
                                 ExtString As String, DestFolder As String)
    Dim ns As Namespace
    Dim Inbox As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim MyDocPath As String
    Dim I As Integer
    Dim wsh As Object
    Dim fs As Object

    On Error GoTo ThisMacro_err

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder = Inbox.Folders(OutlookFolderInInbox)

    I = 0
    ' Check subfolder for messages and exit of none found
    If SubFolder.Items.Count = 0 Then
        MsgBox "There are no messages in this folder : " & OutlookFolderInInbox, _
               vbInformation, "Nothing Found"
        Set SubFolder = Nothing
        Set Inbox = Nothing
        Set ns = Nothing
        Exit Sub
    End If

    'Create DestFolder if DestFolder = ""
    If DestFolder = "" Then
        Set wsh = CreateObject("WScript.Shell")
        Set fs = CreateObject("Scripting.FileSystemObject")
        MyDocPath = wsh.SpecialFolders.Item("mydocuments")
        DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
        If Not fs.FolderExists(DestFolder) Then
            fs.CreateFolder DestFolder
        End If
    End If

    If Right(DestFolder, 1) <> "\" Then
        DestFolder = DestFolder & "\"
    End If

    ' Check each message for attachments and extensions
    For Each Item In SubFolder.Items
        For Each Atmt In Item.Attachments
            If LCase(Right(Atmt.FileName, Len(ExtString))) = LCase(ExtString) Then
                FileName = DestFolder & Item.SenderName & " " & Atmt.FileName
                Atmt.SaveAsFile FileName
                I = I + 1
            End If
        Next Atmt
    Next Item

    ' Show this message when Finished
    If I > 0 Then
        MsgBox "You can find the files here : " _
             & DestFolder, vbInformation, "Finished!"
    Else
        MsgBox "No attached files in your mail.", vbInformation, "Finished!"
    End If

    ' Clear memory
ThisMacro_exit:
    Set SubFolder = Nothing
    Set Inbox = Nothing
    Set ns = Nothing
    Set fs = Nothing
    Set wsh = Nothing
    Exit Sub

    ' Error information
ThisMacro_err:
    MsgBox "An unexpected error has occurred." _
         & vbCrLf & "Please note and report the following information." _
         & vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
         & vbCrLf & "Error Number: " & Err.Number _
         & vbCrLf & "Error Description: " & Err.Description _
         , vbCritical, "Error!"
    Resume ThisMacro_exit

End Sub