附件中的附件

时间:2016-03-04 20:38:35

标签: vba outlook-vba email-attachments

我找到了几个关于如何抓取电子邮件附件的页面,但没有专门针对我需要的内容。

我偶尔会收到包含其他几封电子邮件作为附件的电子邮件,而且这些附加的电子邮件中的每一封都包含我想要放在我桌面上的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

请注意 msg 是包含其他电子邮件的 MailItem

这也是我第一次使用For Each循环,因此也可能是一个问题,但是现在我只想获得正确的PDF逻辑。

我认为问题在于 outAtmt MailItem ,但我不知道其他任何方式。< / p>

1 个答案:

答案 0 :(得分: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