【发布时间】:2021-06-15 23:00:09
【问题描述】:
我找到了检索附件的代码:https://www.rondebruin.nl/win/s1/outlook/saveatt.htm
我对其进行了改进,将发件人信息作为可选过滤输入考虑在内:
Sub SaveEmailAttachmentsToFolder(OutlookFolderInInbox As String, _
ExtString As String, DestFolder As String, _
Optional Filter As String = "[SenderEmailAddress] = 's@example.com'")
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 MyDocPath As String
Dim I As Integer
Dim wsh As Object
Dim fs As Object
On Error GoTo ThisMacro_err
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders(OutlookFolderInInbox)
I = 0
' Check subfolder for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in this folder : " & OutlookFolderInInbox, _
vbInformation, "Nothing Found"
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Exit Sub
End If
'Create DestFolder if DestFolder = ""
If DestFolder = "" Then
Set wsh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
MyDocPath = wsh.SpecialFolders.Item("mydocuments")
DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
If Not fs.FolderExists(DestFolder) Then
fs.CreateFolder DestFolder
End If
End If
If Right(DestFolder, 1) <> "\" Then
DestFolder = DestFolder & "\"
End If
' Check each message for attachments and extensions
For Each Item In SubFolder.Items.Restrict(Filter)
For Each Atmt In Item.Attachments
If LCase(Right(Atmt.FileName, Len(ExtString))) = LCase(ExtString) Then
FileName = DestFolder & Item.SenderName & " " & Atmt.FileName
Atmt.SaveAsFile FileName
I = I + 1
End If
Next Atmt
Next Item
' Show this message when Finished
If I > 0 Then
MsgBox "You can find the files here : " _
& DestFolder, vbInformation, "Finished!"
Else
MsgBox "No attached files in your mail.", vbInformation, "Finished!"
End If
' Clear memory
ThisMacro_exit:
Set SubFolder = Nothing
Set Inbox = Nothing
Set ns = Nothing
Set fs = Nothing
Set wsh = Nothing
Exit Sub
' Error information
ThisMacro_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: SaveEmailAttachmentsToFolder" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume ThisMacro_exit
End Sub
我想进一步完善它,将邮件从 s@example.com 发送到其他人。
有没有办法检查邮件是否发送到 s@example.com 然后转发给我?如果可以,我可以获取发送原始邮件的人的电子邮件地址吗?
我只想下载最初由某些电子邮件地址发送的附件。
另一种解释方式:
- A1、A2、A3、A4...向 B 发送邮件。B 将这些邮件转发给我
- B 给我发的邮件不是来自其他人的邮件
- 如果是B转发的,检查原发件人
- 如果发件人在我输入的发件人数组(例如 A1、A4)中,请下载附件
【问题讨论】:
-
检查您的电子邮件的源代码(见How To View Source Code Of Html Email Body In Outlook?)如果您可以通过任何类型的数据看到电子邮件被转发,那么这是可能的,否则是不可能的。
-
Len(MailItem.ConversationIndex) > 44 表示回复或转发stackoverflow.com/questions/36412152/…
-
对于原始发件人,您可以向上搜索“发件人:”stackoverflow.com/questions/20001670/…