Auto Download Outlook Email Attachment – Code in VBA by Topbullets.com

Topbullets.comI get some raw data through email every day. Now when I want to do some analysis on the data I need to go through the inbox, search for that particular data and then download it to local. The process is very tedious. Now I am very happy that I have got rid of this manual process. Kudos to Google! I want to share the tutorial with very basic code. One can easily leverage the VBA code to write some complicated criteria and enhance it further. Here I am using snapshots from Outlook 2013 but have tested the feature in 2007 and was working fine. Without talking too much let’s start.

Step 1: Open your Outlook and enable micro.

File>>Option>> Trust Center>> Trust center settings>> Select notification for all macros

TrustCenter

Step 2: Then press Alt+F11 to open VBA. Go to ‘Module’ from left panel or you can add one new one if you don’t see it there.

Step 3: Paste the below code and save it.

Public Sub SaveAutoAttach(item As Outlook.MailItem)

Dim object_attachment As Outlook.Attachment

Dim saveFolder As String
' Folder location when I want to save my file
saveFolder = "D:\Data\Archive"

    For Each object_attachment In item.Attachments
' Criteria to save .doc files only
    If InStr(object_attachment.DisplayName, ".doc") Then

        object_attachment.SaveAsFile saveFolder & "\" & object_attachment.DisplayName

    End If

    Next

End Sub

Step 4: Now Go to Rule>>Manage Rules & Alerts. Please follow below snapshots.

Pic1

Step 5: Select ‘Apply rule on message I receive’ and Next

Pic2

Step 6: In this step choose your words sincerely. Right now I am choosing 2nd option. Whatever you select on the first panel you will see them in 2nd panel. Click on ‘Specific word’ circled in the image.

Pic3

Step 7: Provide the word like “IBM Data” and save it. You can add more words as well.
Pic4

Step 8: In this step we will select ‘Action’. As we want to run a VBA script, we select ‘run a script’. Again click on ‘run a script’; a pop-up windows may open. Just ‘Enable’ the micros and select the micro which we saved in backend.

Pic5

Pic6

Step 9: You are almost done. Save the rule as per your convenience and click ‘Finish’. Select the circled check box if you want to run the script on deleted items otherwise it’s good.
Pic7

Step 10: Restart your outlook and send some document with subject ‘IBM Data’ and see the magic.

Signature

Deepesh Singh
logo

Advertisement

23 thoughts on “Auto Download Outlook Email Attachment – Code in VBA by Topbullets.com

  1. Hi I have pasted the above code in outlook module & when i setting rule, i am not getting open of “Run a script”.
    how can i find it, let me know if there is any option to show it.

  2. hi, i was curious on how the code would fit ( InStr ) if you wanted to have it for more than two file extenstions.
    Like .xlsx or .pdf along with .doc put in.

    • Thank you so much for instruction auto saving attachments from outlook to specific folder. Such a time saver.
      Just one more thing how can i add several other criteria to same VBA project or create a new project?

  3. very useful code, but i have problem
    problem is file send with same file name so old file deleted and replaced by new one.
    need to add date for file name
    thanks for you

    • try this:

      Public Sub SaveAutoAttach(item As Outlook.MailItem)

      Dim object_attachment As Outlook.Attachment

      Dim FileName As String

      Dim saveFolder As String
      ‘ Folder location when I want to save my file
      saveFolder = “D:\Data\Archive”

      For Each object_attachment In item.Attachments
      ‘ Criteria to save .pdf files only
      If InStr(object_attachment.DisplayName, “.pdf”) Then

      FileName = Replace(object_attachment.DisplayName, “.pdf”, “”)

      object_attachment.SaveAsFile saveFolder & “\” & FileName & ” ” & Format(Now(), “DD-MMM-YYYY hh mm AMPM”) & “.pdf”

      End If

      Next

      End Sub

    • Try this:

      Public Sub SaveAutoAttach(item As Outlook.MailItem)

      Dim object_attachment As Outlook.Attachment

      Dim FileName As String

      Dim saveFolder As String
      ‘ Folder location when I want to save my file
      saveFolder = “D:\Data\Archive”

      For Each object_attachment In item.Attachments
      ‘ Criteria to save .pdf files only
      If InStr(object_attachment.DisplayName, “.pdf”) Then

      FileName = Replace(object_attachment.DisplayName, “.pdf”, “”)

      object_attachment.SaveAsFile saveFolder & “\” & FileName & ” ” & Format(Now(), “DD-MMM-YYYY hh mm AMPM”) & “.pdf”

      End If

      Next

      End Sub

  4. Ahmed, i had the same problem. I found that this worked for me:

    Public Sub SaveAutoAttach(item As Outlook.MailItem)

    Dim object_attachment As Outlook.Attachment

    Dim FileName As String

    Dim saveFolder As String
    ‘ Folder location when I want to save my file
    saveFolder = “D:\Data\Archive”

    For Each object_attachment In item.Attachments
    ‘ Criteria to save .pdf files only
    If InStr(object_attachment.DisplayName, “.pdf”) Then

    FileName = Replace(object_attachment.DisplayName, “.pdf”, “”)

    object_attachment.SaveAsFile saveFolder & “\” & FileName & ” ” & Format(Now(), “DD-MMM-YYYY hh mm AMPM”) & “.pdf”

    End If

    Next

    End Sub

  5. In Step 8 :
    After clicking on ‘a script’ , Not Able to see saved VBA script in “Select a script” Option.
    Plz help…!!!

  6. Edited to save the file with the date that was sent on the file.

    Public Sub SaveAutoAttach(item As Outlook.MailItem)

    Dim object_attachment As Outlook.Attachment

    Dim FileName As String

    Dim saveFolder As String
    ‘ Folder location when I want to save my file
    saveFolder = “D:\Data\Archive\”

    For Each object_attachment In item.Attachments
    ‘ Criteria to save .pdf files only
    If InStr(object_attachment.DisplayName, “.pdf”) Then

    FileName = Replace(object_attachment.DisplayName, “.pdf”, “”)

    object_attachment.SaveAsFile saveFolder & “\” & FileName & ” ” & Format(item.SentOn(), “YYYY-MM-DD hh.mm AMPM”) & “.pdf”

    End If

    Next

    End Sub

Please leave your valuable comment.

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s