我的VBA代码将附件从电子邮件下载到本地驱动器。我想要重命名附件,使其包含日期。日期应为收到电子邮件的前一天。
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发布于 2019-09-26 01:18:55
已更新下面的代码,以便在显示文件名之前追加日期。为此,我们使用DATEADD并将-1天添加到接收日期,并使用"-"s而不是"/"s将DateTime值FORMATting成一个日期值。
如果您希望将其添加到文件名之后,但在扩展名之前,我们需要解析为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 Subhttps://stackoverflow.com/questions/58097583
复制相似问题