2013-10-08 457 views
1

我試圖將附件中帶有.eml消息的文件夾全部提取出來,然後在其他文件夾中提取/重命名/保存附件。我的代碼:VBA/Outlook從.eml文件中提取附件

Sub SaveAttachments() 
    Dim OlApp As Outlook.Application 
    Set OlApp = GetObject(, "Outlook.Application") 
    Dim MsgFilePath 
    Dim Eml As Outlook.MailItem 
    Dim att As Outlook.Attachments 
    Dim Path As String 
    Path = "C:\Users\richard\Desktop\Inbox\" 

    If OlApp Is Nothing Then 
     Err.Raise ERR_OUTLOOK_NOT_OPEN 
    End If 

    Dim fs As Object 
    Set fs = CreateObject("Scripting.FileSystemObject") 
    Dim temp As Object 
    Set temp = fs.GetFolder(Path) 

    For Each MsgFilePath In temp.Files 
     Set Eml = OlApp.CreateItemFromTemplate(Path & MsgFilePath.Name) 

    Set att = Eml.Attachments 
     If att.Count > 0 Then 
      For i = 1 To att.Count 
       fn = "C:\Users\richard\Desktop\cmds\" & Eml.SenderEmailAddress 
       att(i).SaveAsFile fn 
      Next i 
     End If 


     Set Eml = Nothing 
    Next 

    Set OlApp = Nothing 
End Sub 

但我發現了通俗易懂這個錯誤在循環中的第一個文件,即行 集EML = OlApp.CreateItemFromTemplate(路徑& MsgFilePath.Name):

-2147286960 (80030050) %1 already exists. 

關於正在進行的任何想法,非常感謝!

+0

鳥類景觀:既然你提到的MOT哪條線路,是此行'ATT(I).SaveAsFile fn'? –

+0

此外,如果有幾個來自同一個發件人的電子郵件,那麼你的代碼將嘗試覆蓋文件...''C:\ Users \ richard \ Desktop \ cmds \「&Eml.SenderEmailAddress' –

+0

感謝您的建議 - 我可以確認錯誤發生在FIRST循環(所以沒有其他文件尚未打開/創建),並且這是爲了防止所有電子郵件與地址不同。已更新問題以顯示導致錯誤的行 – Petrov

回答

2

試試這個(久經考驗的)

Private Declare Function ShellExecute Lib "shell32.dll" Alias _ 
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _ 
String, ByVal lpFile As String, ByVal lpParameters As String, _ 
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long 

Private Const SW_SHOWNORMAL As Long = 1 
Private Const SW_SHOWMAXIMIZED As Long = 3 
Private Const SW_SHOWMINIMIZED As Long = 2 

Sub SaveAttachments() 
    Dim OlApp As Outlook.Application 
    Set OlApp = GetObject(, "Outlook.Application") 
    Dim MsgFilePath 
    Dim Eml As Outlook.MailItem 
    Dim att As Outlook.Attachments 
    Dim sPath As String 
    sPath = "C:\Users\richard\Desktop\Inbox\" 

    If OlApp Is Nothing Then 
     Err.Raise ERR_OUTLOOK_NOT_OPEN 
    End If 

    sFile = Dir(sPath & "*.eml") 

    Do Until sFile = "" 
     ShellExecute 0, "Open", sPath & sFile, "", sPath & sFile, SW_SHOWNORMAL 

     Wait 2 

     Set MyInspect = OlApp.ActiveInspector 
     Set Eml = MyInspect.CurrentItem 

     Set att = Eml.Attachments 
     If att.Count > 0 Then 
      For i = 1 To att.Count 
       fn = "C:\Users\richard\Desktop\cmds\" & i & "-" & Eml.SenderEmailAddress 
       att(i).SaveAsFile fn 
      Next i 
     End If 

     sFile = Dir$() 
    Loop 

    Set OlApp = Nothing 
End Sub 

Private Sub Wait(ByVal nSec As Long) 
    nSec = nSec + Timer 
    While nSec > Timer 
     DoEvents 
    Wend 
End Sub 
+0

感謝您的這一點,但仍然得到相同的錯誤。我只是注意到文件夾中的消息是以.eml格式 - 也許這是問題的根源? – Petrov

+0

將sFile = Dir(sPath&「* .msg」)改爲'sFile = Dir(sPath&「*。*」)',然後在'Do Until sFile ='之後插入這一行'Msgbox sFile''「你看到的msgbox? –

+0

我得到文件夾中的第一個文件的名稱,加上擴展名例如。 foob​​ar.eml – Petrov