0

我写了一些 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)。

4

1 回答 1

1

您的过程很可能在尝试将附件保存到不存在的文件夹时遇到错误,因为代码不会检查保存附件是否成功或文件夹是否存在。但是,错误被 抑制了On Error Resume Next,所以代码继续删除附件,即使它们没有被保存。除非您有备份,否则附件可能会丢失。

On Error Resume Next除非您确切地知道自己在做什么并且有合理的错误处理代码,否则永远不要使用。

于 2013-07-08T11:09:22.633 回答