Nested email attachments


Outlook version
Outlook 2016 32 bit
Email Account (as MS Exchange)

I've been visiting these forums for some time now, and I've always managed to solve whatever challenges I've had with my code, but I'm slightly stumped with the logic (at least) of what it is I'm trying to do with a macro in Outlook. I would be grateful for any guidance or suggestions, because as much as I google it, I can't quite find the right answer.

I expect the code would be straightforward, but I just can't wrap my head around the logic of it. In essence:

1. User selects a number of email - these emails will each have attachments to them.
2. The emails and the attachments are downloaded to the local drive.
3. HOWEVER, it is likely that these attachments are a mix of both MSG ("Attachment Email") and standard attachments (e.g., DOCX, PDF, etc).
4, The macro then needs to save the attachments to the Attachment Emails, until all the attachments are saved.
5. THEN, all of the Attachment Emails need to be sorted chronologically, and arranged/printed in order. If there is an attachment to an Attachment Email, that will need to sit appended to the corresponding parent email.

Saving the attachments to the hard drive is ok (.SaveAsFile), thank you Slipstick (Saving Attachments to the Hard Drive). I'm stuck on the sorting part. Do I reimport the MSG files back into Outlook? CreateItemFromTemplate and then sort and then re-export?

I feel like I'm over thinking this.

Again, any guidance would be greatly appreciated. Thank you.


Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I dont think importing/exporting will help - its not going to fix windows sorting. Windows sorts by created, saved or mod date - the emails will use the date you saved them. I would get the date from the message and either add it to the filename or update the filesystem properties so you can sort by a property field.


Outlook version
Outlook 2016 32 bit
Email Account (as MS Exchange)
Hi Diane

I only just managed to come back to this forum this evening owing to work demands, so I apologise for not replying earlier.

Thank you for taking the effort to set out your thoughts - as it so happens I stumbled across the perfect solution by accident an hour or so after having posted the question on this forum. The short story is:-

1. I was overthinking it; and
2. You were absolutely right - the key was to add a date/timestamp at the front of filenames (yyyymmdd-hhmmss).

The subroutine works its way through each email in the selection, saves it to the designated folder with a filename composed of the date/time of the email and the subject of email (stripped of illegal characters): (yyyymmdd-hhmmss_SubjectOfEmail.msg).

If that email had any attachments, then those attachments are saved to the designated folder with a filename composed of the date/time of the parent email and then its actual filename unless the relevant attachment was itself an MSG file, in which case it would set about going through the process again recursively, but this time with its own date/time as the working date/timestamp. The final result was several hundred documents in a folder, all perfectly arranged chronologically.

It turns out that the total count was 56 emails (parent emails + nested emails), and the total non-MSG attachments was 228 documents. Needless to say, this saved us a lot of time.

I will look into cleaning up the code and posting it for others to use (or critique). Thank you again.