Samsquatch
Samsquatch

Reputation: 27

Attachments within attachments

I've found several pages on how to grab email attachments but not specifically for what I need.

Occasionally I receive emails that contain several other emails as attachments, and each of those attached emails contain PDFs that I would like to throw on my desktop somewhere.

This is as far as I've gotten:

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

Note that msg is the MailItem that contains the other emails.

This is also my first time using the For Each loop so that might be a problem too, but for right now I just want to get the logic of getting that PDF right.

I think the problem is is that outAtmt is a MailItem, but I don't know any other way around it.

Upvotes: 2

Views: 171

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

You would need to open the saved MSG file using Namespace.GetSharedItem and then process its attachments.

If you want to avoid having to save the embedded message attachment, you can use Redemption (I am its author) which exposes the RDOAttachment.EmbeddedMsg property (returns RDOMail object):

  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

Upvotes: 1

Related Questions