Automatically Save Email Attachments following a naming convention

Have you ever have a client that will only send you data as Email attachments, no ftp, etc,… I had the same case, but did not have direct access to the exchange server and did not have the time to spend researching a sexier way to automatically download their attachments.

This can easily be enhanced to do only messages from specific senders, other folders, etc. However the following code is what I used to perform one click save of all files to a network share, which then could be picked up by SSIS.

Sub SaveOutlookFileAttachments()
Dim oStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oFolders As Outlook.Folders
Dim oFolder As Outlook.Folder
Dim destFolder As String
Dim oItems As Outlook.Items
Dim oMsg As Outlook.MailItem
Dim oAttachments As Outlook.Attachments
Dim oAttachment As Outlook.Attachment
Dim oExplorer As Outlook.Explorer
destFolder = "\\NetworkShare\OrderDetailReport\"

On Error Resume Next
Set oStores = Application.Session.Stores
For Each oStore In oStores
If oStore.DisplayName = "Inbox" Then
oFolders = oStore.GetSearchFolders
For Each oFolder In oFolders
oItems = oFolder.Items
For Each oMsg In oItems
oAttachments = oMsg.Attachments
For Each oAttachment In oAttachments
If InStr(1, oAttachment.FileName, "_orderdetailreport_", vbTextCompare) Then
'MsgBox ("This File Needs to be Saved: " & oAttachment.FileName)
oAttachment.SaveAsFile (destFolder & oAtch.DisplayName)
End If
End If
End Sub


4 thoughts on “Automatically Save Email Attachments following a naming convention

  1. Hi John,
    forgive my ignorance… where do I run your code from to have attachements saved? It’s not an SSIS Script Task code, is it?

  2. Mr Acosta…CAN this code be used in an SSIS Script task? I’ve got a project where I need to extract an Excel file in an Outlook attachment received weekly to a share and create a report from it. If I can use your code in a Script task I can totally automate the process. (I’m gonna see what I can do with your code anyhow, but it would be nice to know going into the work if it’s feasible to do or not.)

    And thanks for the code – it looks useful even if I can’t get it to work in a Script task.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s