在文件名上使用 ReceivedTime 保存来自 Outlook 的附件

Posted

技术标签:

【中文标题】在文件名上使用 ReceivedTime 保存来自 Outlook 的附件【英文标题】:Save Attachments from Outlook with ReceivedTime on file name 【发布时间】:2016-05-03 16:16:49 【问题描述】:

我正在尝试创建一个可以保存电子邮件附件的宏。我目前遇到的问题是我希望宏在它保存的文件名上添加电子邮件的 ReceivedTime(即:文件 TESTSHEET.xls 于 2016 年 1 月 1 日上午 3:02 收到。我想要保存的文件显示 201601010302AM-TESTSHEET.xls 或类似的东西)

这是我当前的代码:

Public itm As Object

Public Sub saveAttachtoDisk()
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
        saveFolder = "C:\Users\Username\Documents\TEST REPORTS"
        For Each objAtt In itm.Attachments
        objAtt.SaveAsFile objAtt.DisplayName
    Next objAtt
End Sub

Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim objDate As String
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim StrDate As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Set itm = Application.CreateItem(olMailItem)
Dim CurrentMsg As Outlook.MailItem


 ' Get the path to your My Documents folder
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' Set the Attachment folder.
strFolderpath = strFolderpath & "\TEST REPORTS\"

' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For Each objMsg In objSelection

    ' This code only strips attachments from mail items.
    ' If objMsg.class=olMail Then
    ' Get the Attachments collection of the item.
    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
    strDeletedFiles = ""

    If lngCount > 0 Then

        ' We need to use a count down loop for removing items
        ' from a collection. Otherwise, the loop counter gets
        ' confused and only every other item is removed.

        For i = lngCount To 1 Step -1

            ' Save attachment before deleting from item.
            ' Get the file name.
            strFile = objAttachments.Item(i).FileName

            ' Combine with the path to the Save folder.
            StrDate = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
            strFile = strFolderpath & StrDate & strFile

            ' Save the attachment as a file.
            MsgBox strFile
            objAttachments.Item(i).SaveAsFile strFile

            ' Delete the attachment.
            objAttachments.Item(i).Delete

            'write the save as path to a string to add to the message
            'check for html and use html tags in link
            If objMsg.BodyFormat <> olFormatHTML Then
                strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
            Else
                strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
                strFile & "'>" & strFile & "</a>"
            End If

            'Use the MsgBox command to troubleshoot. Remove it from the final code.
            'MsgBox strDeletedFiles

        Next i

        ' Adds the filename string to the message body and save it
        ' Check for HTML body
        If objMsg.BodyFormat <> olFormatHTML Then
            objMsg.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
        Else
            objMsg.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & objMsg.HTMLBody
        End If
        objMsg.Save
    End If
Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub

提前感谢您的帮助!!

【问题讨论】:

我们知道您想让它做什么,但是这段代码做什么 做什么? 文件名前缀格式应使用"yyyymmddhhmm-"。但是您应该再次检查是否存在结果名称的文件。此外,如果要在 Outlook 中执行此宏,则不需要 Outlook 应用程序。 【参考方案1】:

使用 objMsg 而不是它。

' StrDate = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
 StrDate = Format(objMsg.ReceivedTime, "yyyy-mm-dd Hmm ")

另外使用 itm 和 objOL 删除其他代码

' Set itm = Application.CreateItem(olMailItem)
' Dim CurrentMsg As Outlook.MailItem

' On Error Resume Next
' Instantiate an Outlook Application object.
' Set objOL = CreateObject("Outlook.Application")

在您知道自己在做什么之前,请勿使用 On Error Resume Next。

【讨论】:

以上是关于在文件名上使用 ReceivedTime 保存来自 Outlook 的附件的主要内容,如果未能解决你的问题,请参考以下文章

保存电子邮件附件时出现运行时错误 '91

同时录制来自两个输入设备的音频并保存到波形文件中?

如何将来自 Firebase 的 pdf 文件保存在网络的本地文件夹中

将文件保存在设备存储上

如何返回最近收到的电子邮件?

在 VSCode 中本地保存来自远程服务器的数据帧