Import Email Attachments from Outlook into Excel Automatically

How to import email attachments from Outlook into an Excel worksheet automatically using VBA. Here’s the complete VBA code:
Option Explicit
Const AttachmentPath As String = “C:\myattachments\”
Sub GetFromOutlook2()
Dim outlookAtch As Object
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, “DD-MM-YYYY”) & “-”
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim col As Long
col = 0
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace(“MAPI”)
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders(“CJ”)

i = 1

For Each OutlookMail In Folder.Items
If OutlookMail.Attachments.Count > 0 Then
For Each outlookAtch In OutlookMail.Attachments
If OutlookMail.ReceivedTime >= Range(“start_Date”).Value Then
Range(“email_Subject”).Offset(i, 0).Value = OutlookMail.Subject
Range(“email_Date”).Offset(i, 0).Value = OutlookMail.ReceivedTime
Range(“email_Sender”).Offset(i, 0).Value = OutlookMail.SenderName
Range(“email_Body”).Offset(i, 0).Value = OutlookMail.Body
outlookAtch.SaveAsFile NewFileName & outlookAtch.Filename
Range(“email_attachment”).Offset(i, col).Value = outlookAtch
col = col + 1

End If

Next outlookAtch

col = 0
i = i + 1
End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.