2013-07-08 73 views
0

失蹤我寫了一些VBA宏下載我outlook.I的所有附件使用下面的代碼來實現這一目標:附件在Outlook 2010中

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.....文件夾中,這個文件夾實際上並不存在於我的系統中。我需要這些附件,它們非常重要。

有什麼方法可以恢復這些郵件附件。 (我猜,附件已從服務器本身刪除,因爲我的代碼objAttachments.Item(i).Delete中有一個聲明)。

回答

1

當試圖將附件保存到不存在的文件夾時,很可能您的過程遇到錯誤,因爲代碼不檢查保存附件是否成功或文件夾是否存在。但是,錯誤被On Error Resume Next壓制,所以代碼繼續刪除附件,即使它們尚未保存。除非您有備份,否則附件可能會丟失。

NEVER使用On Error Resume Next除非你知道確切你在做什麼有合理的錯誤處理代碼到位。