【发布时间】: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