【问题标题】:How do I add date to attachment name?如何在附件名称中添加日期?
【发布时间】:2020-01-25 14:21:21
【问题描述】:

我的 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

【问题讨论】:

    标签: vba outlook attachment


    【解决方案1】:

    更新了下面的代码,在显示文件名之前附加日期。我们通过使用 DATEADD 并将 -1 天添加到接收日期并使用“-”而不是“/”将日期时间值格式化为日期值来做到这一点。

    如果您希望在文件名之后、扩展名之前添加它,我们需要解析为文件名。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2010-12-20
      • 2021-12-17
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      相关资源
      最近更新 更多