附件中的附件

Attachments within 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 的逻辑。

我认为问题在于 outAtmtMailItem,但我不知道知道任何其他解决方法。

您需要使用 Namespace.GetSharedItem 打开已保存的 MSG 文件,然后处理其附件。

如果您想避免必须保存嵌入的邮件附件,您可以使用 Redemption (I am its author) which exposes the RDOAttachment.EmbeddedMsg property (returns 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