VBA - Outlook attachments

zJenkins

Board Regular
Joined
Jun 5, 2015
Messages
148
Hi,

I receive multiple emails daily with attached .csv files and am looking for a macro to save the files to a folder location based on the email subject (file name is auto generated and changes daily so I can't use that). The files received currently do not go to my inbox but are directed to a sub folder in Outlook so I need to be able to pull the attachments from the sub folder.

I have a few years experience with VBA, but not much as it relates to Outlook, so any direction would be helpful

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If you're just trying to save the files, it sounds like you would run the code from Outlook, not Excel.

Some topics to start off with some internet searching to find examples:
1. outlook vba selected email
2. outlook vba save attachments

From 1), Slipstick.com has a good all-in-one example. You would need to select the email(s) in your subfolder that have the attachments you want to save and change the location of the save location (strFolderpath) maybe based on parsing the Subject data (SpecialFolders(16) in the code is the Documents folder (e.g., what used to be My Documents)) and finding the target location by VBA techniques.
VBA Code:
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String

    ' Get the path to your My Documents folder
    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
    On Error Resume Next

    ' Instantiate an Outlook Application object.
    Set objOL = Application

    ' Get the collection of selected objects.
    Set objSelection = objOL.ActiveExplorer.Selection

' The attachment folder needs to exist
' You can change this to another folder name of your choice

    ' Set the Attachment folder.
    strFolderpath = strFolderpath & "\OLAttachments\"

    ' Check each selected item for attachments. 
    For Each objMsg In objSelection

    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
        
    If lngCount > 0 Then
    
    ' Use a count down loop for removing items
    ' from a collection. Otherwise, the loop counter gets
    ' confused and only every other item is removed.
    
    For i = lngCount To 1 Step -1
    
    ' Get the file name.
    strFile = objAttachments.Item(i).FileName
    
    ' Combine with the path to the Temp folder.
    strFile = strFolderpath & strFile
    
    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile
    
    Next i
    End If
    
    Next
    
ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub

If you are wanting to save the attachments for emails in the subfolder without selecting them, then you will have to search for examples ("outlook vba subfolder").
 
Upvote 0
Solution
Hey, thanks for the input! I’d never considered doing it from Outlook…figured it was possible just never had before. Thanks!!
 
Upvote 0
I have been looking at this for hours and cannot get this run. any help?
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top