Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Last active May 29, 2019 05:33
Show Gist options
  • Save DataSolveProblems/3a95b34778f5be88b131dce5913ebdb1 to your computer and use it in GitHub Desktop.
Save DataSolveProblems/3a95b34778f5be88b131dce5913ebdb1 to your computer and use it in GitHub Desktop.
Sub Download_Attachments()
Dim ns As NameSpace
Dim olFolder_Inbox As Folder
Dim olMail As MailItem
Dim olAttachment As Attachment
Dim fso As Object
Dim Files_Saved_Folder_Path As String
Files_Saved_Folder_Path = "<Your folder path>"
Set ns = GetNamespace("MAPI")
Set fso = CreateObject("Scripting.FileSystemObject")
Set olFolder_Inbox = ns.GetDefaultFolder(olFolderInbox)
For Each olMail In olFolder_Inbox.Items
If TypeName(olMail) = "MailItem" And olMail.Attachments.Count > 0 Then
fso.CreateFolder (fso.BuildPath(Files_Saved_Folder_Path, Trim(olMail.Subject)))
For Each olAttachment In olMail.Attachments
Select Case UCase(fso.GetExtensionName(olAttachment.FileName))
Case "XLSX", "XLSM"
olAttachment.SaveAsFile fso.BuildPath(Files_Saved_Folder_Path, Trim(olMail.Subject)) & "\" & olAttachment.FileName
Case "JPEG", "PNG", "JPG"
'olAttachment.SaveAsFile fso.BuildPath(Files_Saved_Folder_Path, Trim(olMail.Subject)) & "\" & olAttachment.FileName
Case Else
'skip
End Select
Next olAttachment
End If
Next olMail
Set olFolder_Inbox = Nothing
Set fso = Nothing
Set ns = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment