Reputation: 27
My VBA code downloads attachments from emails to my local drive. I would like to rename the attachments to include the date. The date should be the day before the email was received.
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 = "\\gbhxxxx\Groups\Shared\EBS\Post Go-Live\Auto MT940 download Test"
For Each object_attachment In item.Attachments
' Criteria to save .940 files only
If InStr(object_attachment.DisplayName, "UKAutoMT940") Then
object_attachment.SaveAsFile saveFolder & "\" & object_attachment.DisplayName
End If
Next
End Sub
Upvotes: 0
Views: 898
Reputation: 644
Updated your code below to append the date BEFORE the display file name. We do this by using DATEADD and adding -1 days to the recieved date and FORMATting the datetime value into a date value with "-"s instead of "/"s.
If you're looking to add it AFTER the filename but before the extension, we'll need to parse to filename.
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 = "\\gbhxxxx\Groups\Shared\EBS\Post Go-Live\Auto MT940 download Test"
For Each object_attachment In item.Attachments
' Criteria to save .940 files only
If InStr(object_attachment.DisplayName, "UKAutoMT940") Then
object_attachment.SaveAsFile saveFolder & "\" & Format(DateAdd("d", -1, item.ReceivedTime), "dd-mm-yyyy") & "_" & object_attachment.DisplayName
End If
Next
End Sub
Upvotes: 1