Outlook VBA -- Save attachments

I need to loop through all the emails in the folder inbox/projects, and if the email has an attachment, I need to save the attachment in the folder \\OWNER-PC\Users\Owner\Documents\Clients.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try
Sub macro()
Dim objNS As Outlook.Namespace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("projects")
Dim Item As Object
DestFolder = "\\OWNER-PC\Users\Owner\Documents\Clients\"
For Each Item In olFolder.Items
    If TypeOf Item Is Outlook.MailItem Then
        Dim oMail As Outlook.MailItem
        Set oMail = Item
        For Each Atmt In oMail.Attachments
                Filename = DestFolder & Atmt.Filename
                Atmt.SaveAsFile Filename
        Next Atmt

    End If

End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I used this a few years ago.

1. Show the Developer tab in Outlook
      a. File, Options, Customize Ribbon, Select Developer, select OK
2. Add new module and insert code
      a. Select Developer Tab
      b. Select Visual Basic
      c. Select Insert, Select module
      d. Paste code below into module
      e. Close VB
3. In Windows Explorer, create the folder D:\UPSData\EmailAttachments
4. To run the code, in Outlook select Macros from the developer tab, select macros, select the GetAttachments macro and select Run
Sub GetAttachments()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro. (D:\UPSData\EmailAttachments)
    On Error GoTo GetAttachments_err
' Declare variables
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0
' Check Inbox for messages and exit of none found
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the Inbox.", vbInformation, _
               "Nothing Found"
        Exit Sub
    End If
' Check each message for attachments
    For Each Item In Inbox.Items
' Save any attachments found
        For Each Atmt In Item.Attachments
        ' This path must exist! Change folder name as necessary.
        On Error Resume Next
            FileName = "D:\UPSData\EmailAttachments\" & Atmt.FileName & " --- " & Item.EntryID & Atmt.FileName
            Atmt.SaveAsFile FileName
            i = i + 1
         Next Atmt
    Next Item
    On Error GoTo 0
' Show summary message
    If i > 0 Then
        MsgBox "I found " & i & " attached files." _
        & vbCrLf & "I have saved them into the D:\upsdata\EmailAttachments folder." _
        & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
        MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
    End If
' Clear memory
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub
' Handle errors
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information." _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume GetAttachments_exit
End Sub
rrhandle8Author Commented:
There is no IF to go with the END IF inside the loop.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

I see IFs

    If Inbox.Items.Count = 0 Then
         MsgBox "There are no messages in the Inbox.", vbInformation, _
                "Nothing Found"
         Exit Sub
     End If

    If i > 0 Then
         MsgBox "I found " & i & " attached files." _
         & vbCrLf & "I have saved them into the D:\upsdata\EmailAttachments folder." _
         & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
         MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
     End If
my first version has already been corrected (see code in my message)
rrhandle8Author Commented:
Rgonzo1971 -- There is an IF statement missing.
rrhandle8Author Commented:
Looks like I have two good solutions.  I have to left for a few minutes.  Give me some time to test out both.
rrhandle8Author Commented:
Both solutions worked great!  Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.