2016-03-04 69 views
2

我發現了幾個關於如何抓取電子郵件附件的頁面,但並不專門針對我需要的內容。附件中的附件

偶爾我會收到包含其他幾封電子郵件作爲附件的電子郵件,並且每個附加電子郵件都包含我想要放在桌面某處的PDF。

這是據我已經得到了:

If inSubj("TEST_STRING") Then  'One of my functions from earlier in the code 
    Dim atmt As Attachment 
    Dim outAtmt As MailItem  'Outter attachment (mail attachment) 
    Dim inAtmt As Attachment  'Inner attachment (invoice pdf) 
    Dim path As String: path = "C:\SOME_PATH" 

    For Each atmt In msg.Attachments  'Cycle through attachments 
     If atmt.Type = olEmbeddeditem Then 'If attached is a MailItem 
      Set outAtmt = atmt    'Outter attchment = said MailItem 
      For Each inAtmt In outAtmt.Attachments   'Cycle through attachments (the invoices) 
       inAtmt.SaveAsFile (path & inAtmt.FileName) 'Save file 
      Next inAtmt 
     End If 

    Next atmt 
End If 

注意msgMailItem包含其他電子郵件。

這也是我第一次使用For Each循環,因此可能也是一個問題,但現在我只想獲得讓PDF正確的邏輯。

我認爲這個問題是在於outAtmtMailItem,但我不知道它周圍的其他方式。

+0

所以發生了什麼事?你收到和錯誤? – Sorceri

+0

是的,用Set OutAtmt = atmt。可能是因爲不匹配的類型。 – Samsquatch

+0

使用OpenSharedItem http://stackoverflow.com/questions/18723017/how-can-i-handle-the-attachment-of-an-attachment沒有答案的代碼。使用CreateItemFromTemplate http://stackoverflow.com/questions/7890612/vba-code-to-save-an-attachment-excel-file-from-an-outlook-email-that-was-insid-代碼在答案。 – niton

回答

1

您需要使用Namespace.GetSharedItem打開保存的MSG文件,然後處理其附件。

如果你想避免保存嵌入郵件附件,您可以使用Redemption,其暴露RDOAttachment .EmbeddedMsg屬性(返回RDOMail對象):

set Session = CreateObject("Redemption.RDOSession") 
    Session.MAPIOBJECT = Application.Session.MAPIOBJECT 
    set rMsg = Session.GetMessageFromID(msg.EntryID) 
    ProcessAttachments(rMsg) 

    ... 
    sub ProcessAttachments(Msg) 
    For Each atmt In rMsg.Attachments  'Cycle through attachments 
     If atmt.Type = olEmbeddeditem Then 
     ProcessAttachments(atmt.EmbeddedMsg) 
     ElseIf atmt.Type = olByValue Then 
     MsgBox atmt.FileName 
     'atmt.SaveAsFile "c:\temp\" & atmt.FileName 
     End If 
    Next 
    end sub