【问题标题】:Add email subject to file name when saving attachment保存附件时在文件名中添加电子邮件主题
【发布时间】:2022-01-28 10:03:01
【问题描述】:

我的目标是在名为 Infuse Energy Daily Usage Reports 的 Outlook 收件箱子文件夹中提取电子邮件的 .png 文件。

每封电子邮件包含六个png 文件。最大的是我唯一需要的;它正好是 37.6KB。下一个最大的文件是 22.5KB。第三大是 18.2KB。

代码主要满足我的需要。

我想将电子邮件的完整主题添加到文件名的开头。

文件名应为:
“电子邮件主题,创建时间(“yyyymmdd_hhnnss_”),PNG 图像的原始文件名。”

Sub SaveAttachmentsToFolder()
' This Outlook macro checks a named subfolder in the Outlook Inbox
' (here the "Infuse Eneregy Daily Usage Reports" folder) for messages with attached
' files of a specific type (here file with a "png" extension)
' and saves them to disk. Saved files are timestamped. The user
' can choose to view the saved files in Windows Explorer.
' NOTE: make sure the specified subfolder and save folder exist
' before running the macro.
    On Error GoTo SaveAttachmentsToFolder_err
' Declare variables
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
    Dim varResponse As VbMsgBoxResult
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder = Inbox.Folders("Infuse Energy Daily Usage Reports") ' Enter correct subfolder name.
    i = 0
' Check subfolder for messages and exit if none found
    If SubFolder.Items.Count = 0 Then
        MsgBox "There are no messages in the Infuse Energy Daily Usage folder.", vbInformation, _
               "Nothing Found"
        Exit Sub
    End If
' Check each message for attachments
    For Each Item In SubFolder.Items
        For Each Atmt In Item.Attachments
' Check filename of each attachment and save if it has "png" extension
            If Right(Atmt.FileName, 3) = "png" Then
            ' This path must exist! Change folder name as necessary.
                FileName = "C:\Desktop\Energy Comparisons\Infuse Reports (from email)\" & _
                Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
                Atmt.SaveAsFile FileName
                i = i + 1
            End If
        Next Atmt
    Next Item
' Show summary message
    If i > 0 Then
        varResponse = MsgBox("I found " & i & " attached files." _
        & vbCrLf & "I have saved them into the Infuse Reports (from email)." _
        & vbCrLf & vbCrLf & "Would you like to view the files now?" _
        , vbQuestion + vbYesNo, "Finished!")
' Open Windows Explorer to display saved files if user chooses
        If varResponse = vbYes Then
            Shell "Explorer.exe /e,C:\Desktop\Energy Comparisons\Infuse Reports (from email)", vbNormalFocus
        End If
    Else
        MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
    End If
' Clear memory
SaveAttachmentsToFolder_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub
' Handle Errors
SaveAttachmentsToFolder_err:
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information." _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume SaveAttachmentsToFolder_exit
End Sub

【问题讨论】:

  • 使用 "Email Subject" 作为文件名,如果不过滤掉文件名中不允许的字符,将无法正常工作。您需要更换它们或至少将它们剥离。 • 请注意,您只描述了您想要的,但没有描述您的代码有什么问题。有什么错误吗?如果不是,您的代码会做什么而不是您期望它做什么?你的问题到底是什么? • 举例说明您从代码中得到的结果以及您想要的结果可能有助于了解正在发生的事情。
  • 请允许我稍微整理一下我的请求。为了更正我的原始帖子,包含能源报告的电子邮件不包含附件。我目前使用的宏是提取嵌入在电子邮件中的 png 图形,这很好。然而,我的兴趣尤其是一个特定的图形,它显示了一个图表和表格,其中数据使用量以半小时为增量。如果我可以自动提取这些细节,那将是首选。我会更新或在我的帖子中添加评论以提供更多详细信息。

标签: vba outlook email-attachments


【解决方案1】:

首先,不需要遍历文件夹中的所有项目:

 For Each Item In SubFolder.Items
        For Each Atmt In Item.Attachments

这并不是一个好主意,因为遍历文件夹中的所有项目可能需要很长时间。相反,您需要使用 Find/FindNextItems 类的 Restrict 方法。过滤器示例:[Attachment & Subject Like '%keyword%']

Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & _
                   Chr(34) & " Like '%keyword%' AND " & _
                   Chr(34) & "urn:schemas:httpmail:hasattachment" & _
                   Chr(34) & "=1"

在以下文章中详细了解这些方法:

关于保存到磁盘的附件的文件名,在调用SaveAsFile方法之前,需要确保文件名中没有包含禁止符号。

If Right(Atmt.FileName, 3) = "png" Then
            ' This path must exist! Change folder name as necessary.
                FileName = "C:\Desktop\Energy Comparisons\Infuse Reports (from email)\" & Item.Subject & _
                 Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
                Atmt.SaveAsFile FileName
                i = i + 1
            End If

另外请注意,Outlook 文件夹可能包含不同类型的项目。我建议在运行时检查项目的类型,以确保您只处理邮件项目。 Class 属性返回一个 OlObjectClass 常量,指示对象的类。或者只使用以下条件:

If TypeOf Item Is MailItem Then
 ' your code here
End If

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-08-13
  • 2023-03-24
  • 2016-05-31
  • 2013-03-18
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多