我写了一些VBA宏来下载我的outlook的所有附件。我使用以下代码来实现:
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
On Error Resume Next
Set objOL = CreateObject("Outlook.Application")
Set objSelection = objOL.ActiveExplorer.Selection
strFolderpath = strFolderpath & "\Attachments\"
For Each objMsg In objSelection
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = objAttachments.Item(i).FileName
strFile = strFolderpath & strFile
objAttachments.Item(i).SaveAsFile strFile
objAttachments.Item(i).Delete
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile _
& ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" _
& strFile & "'>" & strFile & "</a>"
End If
Next i
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
运行成功。
但我在我的系统中找不到该文件夹。我试图至少在邮件中打开,但是它说附件已保存到我的系统中实际不存在的C:\xxxx.....
文件夹中。我需要那些附件,它们非常重要。
有没有办法恢复这些邮件附件。 (我猜,附件已从服务器本身删除,因为mycode objAttachments.Item(i).Delete
中有声明。)
答案 0 :(得分:1)
尝试将附件保存到不存在的文件夹时,您的程序很可能遇到错误,因为代码不会检查保存附件是否成功或者文件夹是否存在于首位。但是,错误被On Error Resume Next
抑制,因此代码继续删除附件,即使它们尚未保存。除非您有备份,否则附件可能会丢失。
从不使用On Error Resume Next
,除非您知道完全您正在做什么和具有合理的错误处理代码。