【发布时间】:2017-04-21 22:45:36
【问题描述】:
我有 Outlook 2010。我收到的电子邮件具有相同的主题行,并带有要打开的 PDF。当 PDF 打开时,Adobe 询问我是否要将其添加到 Excel 响应文件中,我说是。
当 Adobe 询问是否要添加到响应文件时,我希望它以“好的”响应,但没有它我也能应付。在这一行:
Set SubFolder = Mailbox.Folders("Response File")
我收到一个错误:
尝试的操作失败。找不到对象。
未读电子邮件所在的子文件夹在我的收件箱下方称为“!响应文件”(不带引号)。打开 PDF 后,我想将电子邮件标记为已读,并移至另一个名为“已提取”(不带引号)的子文件夹(在收件箱下)。
Sub GetAttachments()
On Error GoTo GetAttachments_err
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
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Mailbox = Inbox.Parent
Set SubFolder = Mailbox.Folders("!Response File")
i = 0
'check if there is any mail in the folder'
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in the folder.", vbInformation, _
"Nothing Found"
Exit Sub
End If
'Check each message and save the attachment'
If SubFolder.Items.Count > 0 Then
For Each Item In SubFolder.Items
If Item.UnRead = True Then
For Each Atmt In Item.Attachments
FileName = "C:\Users\abrupbac\Desktop\Response Emails\" & Atmt.FileName
Atmt.SaveAsFile FileName 'saves each attachment'
'this code opens each attachment'
Set myShell = CreateObject("WScript.Shell")
myShell.Run FileName
'this sets the email as read'
Item.UnRead = False
'updates the counter'
i = i + 1
Next Atmt
End If
Next Item
End If
'Display results
If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "They are saved on your desktop" _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, _
"Finished!"
End If
'Replenish Memory'
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
'function for sorting the excel attachment'
GetAttachments_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 GetAttachments_exit
End Sub
【问题讨论】: