Hi Guys,
Hope you're doing well. I got a code that downloads the attachments of unread outlook emails to a specified path. However, I would want to rename the attachment same as the email subject.
Would really appreciate your inputs regarding this concern. Thanks in advance!
Below would be my the codes I have.
Sub SaveAttachments()
Dim olApp As Outlook.Application, olNameSpace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder, olMail As Outlook.MailItem
Dim olAttachment As Outlook.Attachment, lngAttachmentCounter As Long
On Error GoTo Oooops
'Create an instance of Outlook and allow the user to choose
'which folder they want to process
Set olApp = New Outlook.Application
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("Adhoc Reports")
If olFolder Is Nothing Then Exit Sub 'User cancelled
For Each olMail In olFolder.Items
If olMail.UnRead = True Then 'If you want ALL messages processed then take this line out and the End If
For Each olAttachment In olMail.Attachments
lngAttachmentCounter = lngAttachmentCounter + 1
olAttachment.SaveAsFile "C:\Users\U0171730\Sandy\Macro Automation Project\" & olAttachment.DisplayName
Next olAttachment
End If
Next olMail
AppActivate "Microsoft Excel"
MsgBox lngAttachmentCounter & " attachments saved.", vbInformation, "Success!"
Exit Sub
Oooops:
MsgBox Err.Description, vbExclamation, "An error occurred"
End Sub
Bookmarks