Link to home
Start Free TrialLog in
Avatar of Nirvana
NirvanaFlag for India

asked on

extract attachments from "eml" files

We receive email which has ".eml" files I need to open "eml" files and extract all the "pdf" attachments.  (i have email folder as "attachments" where i have emails"; I am using office 2016

I am currently saving all the emails to a folder by using macro "https://gallery.technet.microsoft.com/office/Save-attachments-from-5b6bf54b"however I need to extract all pdf files from eml files which are inside ".msg" file

tried this did not work: https://stackoverflow.com/questions/19255083/vba-outlook-extracting-attachments-from-eml-fileshttps://gist.githubusercontent.com/urschrei/5258588/raw/aba67931890a91692e21e9edf45c09d9d1f145ca/parseml.py
Avatar of Bill Prew
Bill Prew

So are you reading EML files, or MSG files from your disk folder?

Can you upload a sample file?

Will Outlook be installed on the machine you want to execute this process on?


»bp
Hello,

In case you already have outlook installed in your PC and your email configured,

Create a New folder in outlook (Optional, you can you existing folder)
Drag and drop the *.mgs file in the created folder (*.msg attache on your original email)
Run a script to extract your attached files (You can find scripts by google I don't have any script for this purpose in my PC)

Good luck Nirvana
Avatar of Nirvana

ASKER

Hi Bill,

Outlook is installed in my machine.

I receive a msg with will have an eml as an attachment and within email there are furthermore attachments(pdfs).

unfortunately i cannot attach any examples as these would have organizational information.
Hi Nirvana,

Are you saying that the email message you receive has an EML attachment within it and, when you open the EML attachment, it is, itself, an email with a PDF attachment, and you want to get that PDF out?

If so, I would do the following:

1) Copy all the messages into a single new folder in Outlook (I'd keep the originals separate and safe).

2) Run a script to extract all the EML attachments to a different new folder.

3) Run a script on the messages (that were originally the EML attachments) to extract the attachments from each of those messages.


If that is correct, and you need help with any of the above steps, post back, and I can help further.

Thanks,

Alan.
Avatar of Nirvana

ASKER

Hi Alan,

Are you saying that the email message you receive has an EML attachment within it and, when you open the EML attachment, it is, itself, an email with a PDF attachment, and you want to get that PDF out?- This is Exactly what I am looking for

Could you please help me on the steps you have provided?
You can try this tool:
https://www.outlookfreeware.com/en/products/all/OutlookConvertEML2PST

They have paid version also, you can contact the support team. I used there tool long back for PF to PST.
Avatar of Nirvana

ASKER

Hi Amit, unfortunately I cannot use 3rd party tools. these are restricted.
Hi Nirvana,

This is what I would do:

1) Copy the emails to a new empty folder in Outlook (keep the originals safe just in case)

2) Create a new empty folder on your computer (I'll assume it is C:\Temp\ for this post and the code I will post below)

3) Select all the items in the Outlook folder and run the code below on them.

4) That should extract all the attachments from those emails - they will all be EML files from what you have said.  It should not impact on the emails in Outlook.  I have told the code to prepend the date / time in standard format to the filenames so that they sort chronologically based on the time of the email message they were in, and (hopefully) avoid any issues if the filenames are all similar or identical.

5) Create another new empty folder in Outlook

6) Go to C:\Temp\ and copy / paste all the EML files from there, into the new empty folder you created in step 5 (Ctrl-A to select all, then Ctrl C to copy, go to the Outlook folder, and Ctrl-V to paste)

7) Delete all the EML files from C:\Temp\ - You don't need them, and you could re-create them if you wanted by doing the steps above again.

8) Go the the new Outlook folder, and run the code below on those messages

9) You should now have all the attachments (PDFs presumably) in C:\Temp\


The code is as follows.  You need to paste this into a new module in Outlook.  To do that:

A) Press Alt + F11 keys to open the VBA Editor (if you get any security warnings, you'll have to click OK or Enable whenever they pop up)

B) In the VBA editor (Window title is 'Microsoft Visual Basic for Applications' click 'Insert' in the menu bar, and choose 'Module'

C) A new module will display

D) Copy / Paste the code below into the module on the right(should be the biggest section on the screen usually)

E) Make sure you have selected all the items in the Outlook folder - if not, go back and do that, before you:

F) Click once in the right side, anywhere in the code you pasted in - just to be sure that part of the windows is selected

G) Run the VBA code by clicking on the 'Run' button in the toolbar (you can also press F5 if you prefer)


That should run the code on the messages you selected in Outlook and drop the attachments into C:\Temp\


Code:

Sub ExtractAttachments()

Dim MyItem As MailItem
Dim MyAtt As Attachment
Dim Location As String
Dim SelectedItems As Variant
Dim NewLocation As String




    Set SelectedItems = ActiveExplorer.Selection

    Location = "C:\Temp\"

       
    For Each MyItem In SelectedItems

        For Each MyAtt In MyItem.Attachments

        MyYear = Year(MyItem.ReceivedTime)
        MyYearStr = CStr(MyYear)
        
        
        MyMonth = Month(MyItem.ReceivedTime)
        MyMonthStr = CStr(MyMonth)
        If MyMonth < 10 Then
            MyMonthStr = "0" & MyMonthStr
        End If
            
            
        MyDay = Day(MyItem.ReceivedTime)
        MyDayStr = CStr(MyDay)
        If MyDay < 10 Then
            MyDayStr = "0" & MyDayStr
        End If
        
        
        MyHour = Hour(MyItem.ReceivedTime)
        MyHourStr = CStr(MyHour)
        If MyHour < 10 Then
            MyHourStr = "0" & MyHourStr
        End If
        
        
        MyMinute = Minute(MyItem.ReceivedTime)
        MyMinuteStr = CStr(MyMinute)
        If MyMinute < 10 Then
            MyMinuteStr = "0" & MyMinuteStr
        End If
        
        MySecond = Second(MyItem.ReceivedTime)
        MySecondStr = CStr(MySecond)
        If MySecond < 10 Then
            MySecondStr = "0" & MySecondStr
        End If
        
        
        
        Date_Time = MyYearStr & MyMonthStr & MyDayStr & " - " & MyHourStr & MyMinuteStr & " - " & MySecondStr & " - "


            MyAtt.SaveAsFile Location & Date_Time & MyAtt.DisplayName



        Next

    Next

End Sub

Open in new window




Hope that makes sense!

If it works, write me a testimonial, if it doesn't, or you have any queries, post back and let me know where it got stuck ;-)

Thanks,

Alan.
Avatar of Nirvana

ASKER

Thanks a Lot Alan, I get the error as User generated image. I have imported all the emails and selected them and ran the code
Hi Nirvana,

Sorry for being slow to respond.

Please can you check that all of the items in the folder are MailItems.

For example, a 'Read Receipt' item will cause the code to stop.

Maybe test it on a single item (just select that one email), run the code, and see if it does what it is supposed to do.

Thanks,

Alan.
Avatar of Nirvana

ASKER

Dear Alan,

It is ok. I understand you must be busy.

The first piece of code works great where i am able to download all eml to one folder (C:\temP)when  I move back to outlook it and extract it again downloads eml fileUser generated imageUser generated image
ASKER CERTIFIED SOLUTION
Avatar of Alan
Alan
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nirvana

ASKER

Hi Alan,
the second solution did not work at least I am able to download all eml. Thanks a lot!!!