• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Extract all attachments from .EML file|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Extract all attachments from .EML file|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosExtract all attachments from .EML f…
sp_PrintTopic sp_TopicIcon
Extract all attachments from .EML file
Avatar
Marsil Sarvis
Member
Members
Level 0
Forum Posts: 73
Member Since:
September 6, 2019
sp_UserOfflineSmall Offline
1
September 16, 2021 - 8:56 am
sp_Permalink sp_Print

Hello Team,

Attached macro work on .msg, please can you help me to update this macro to deal .eml files

Thanks;

Marsil

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
September 19, 2021 - 2:30 pm
sp_Permalink sp_Print

Try this code: (adapted from https://stackoverflow.com/ques.....-eml-files )

#If VBA7 Then
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#Else

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End If
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Sub SaveAttachments()
Dim OlApp As Object
Set OlApp = GetObject(, "Outlook.Application")
Dim MsgFilePath
Dim Eml As Object
Dim att As Object
Dim sPath As String
sPath = "C:\Users\Catalin\Desktop\"

If OlApp Is Nothing Then
Err.Raise ERR_OUTLOOK_NOT_OPEN
End If

sFile = Dir(sPath & "*.eml")

Do Until sFile = ""
ShellExecute 0, "Open", sPath & sFile, "", sPath & sFile, SW_SHOWNORMAL

Wait 2

Set MyInspect = OlApp.ActiveInspector
Set Eml = MyInspect.CurrentItem

Set att = Eml.Attachments
If att.Count > 0 Then
For i = 1 To att.Count
fn = sPath & att(i).DisplayName
att(i).SaveAsFile fn
Next i
End If

sFile = Dir$()
Loop

Set OlApp = Nothing
End Sub

Avatar
Marsil Sarvis
Member
Members
Level 0
Forum Posts: 73
Member Since:
September 6, 2019
sp_UserOfflineSmall Offline
3
September 19, 2021 - 8:46 pm
sp_Permalink sp_Print sp_EditHistory

Thank you Catalin.

I face attached issue, please can you help to solve it.

issue-1.pngImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage issue.png (15 KB)
  • sp_PlupImage issue-1.png (15 KB)
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
September 20, 2021 - 3:16 am
sp_Permalink sp_Print

You have removed the line

Wait 2

 

Here is the missing code, put back the line you removed:

Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub

Avatar
Marsil Sarvis
Member
Members
Level 0
Forum Posts: 73
Member Since:
September 6, 2019
sp_UserOfflineSmall Offline
5
September 21, 2021 - 4:42 am
sp_Permalink sp_Print

Hi Catalin,

Wow! This is awesome and it works like a charm. Thank you so much!!!

I will run this macro on 1000k messages, but I see the macro open the message then extract files then go to second one without close first one.

Please can you let me know that, how I can update attached macro to close each message after extract files.

Thanks;
Marsil

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
September 21, 2021 - 5:06 am
sp_Permalink sp_Print

Try this:

https://stackoverflow.com/ques.....xecute-vb6

Avatar
Marsil Sarvis
Member
Members
Level 0
Forum Posts: 73
Member Since:
September 6, 2019
sp_UserOfflineSmall Offline
7
September 22, 2021 - 2:50 am
sp_Permalink sp_Print sp_EditHistory

Hi Catalin,

Please can you help to update attached macro as I checked stackoverflow URL and I tried many times to update but I couldn't as I face issues.

How kind you are to help me. Thank you very much.

Thanks;

Marisl

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
8
September 22, 2021 - 2:57 pm
sp_Permalink sp_Print

Added this code in a module:

(adapted from http://www.pbdr.com/vbtips/api.....oseAPI.htm)

Option Explicit
#If VBA7 Then
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, _
ByVal aint As Long) As Long
Declare PtrSafe Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare PtrSafe Function EnumWindows Lib "user32" _
(ByVal wndenmprc As LongPtr, ByVal lParam As Long) As Long
Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
#Else
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, _
ByVal aint As Long) As Long
Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function EnumWindows Lib "user32" _
(ByVal wndenmprc As Long, ByVal lParam As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
#End If
Private Const WM_CLOSE = &H10
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDLAST = 1
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDPREV = 3
Private Const GW_OWNER = 4
Private Const GW_CHILD = 5
Private Const GW_MAX = 5

Private mstrTarget As String
Private mblnSuccess As Boolean

Public Function blnFindWindow(strApplicationTitle As String) As Boolean

Dim hWndTmp As Long
Dim nRet As Integer
Dim TitleTmp As String
Dim TitlePart As String
Dim MyWholeTitle As String
Dim mCounter As Long
Dim hWndOver As Integer
Dim sClassName As String * 100

blnFindWindow = False

TitlePart = UCase$(strApplicationTitle)

'loop through all the open windows
hWndTmp = FindWindow(0&, 0&)

Do Until hWndTmp = 0

TitleTmp = Space$(256)
nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))

If nRet Then
'retrieve window title
TitleTmp = UCase$(VBA.Left$(TitleTmp, nRet))

'compare window title & strApplicationTitle
If InStr(TitleTmp, TitlePart) Then
blnFindWindow = True
Debug.Print TitleTmp
Exit Do
End If
End If

hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
mCounter = mCounter + 1

Loop

End Function

Public Function blnCloseWindow(strApplicationTitle As String) As Boolean

' retrieve Windows list of tasks.
mblnSuccess = False
mstrTarget = strApplicationTitle
EnumWindows AddressOf EnumCallback, 0
blnCloseWindow = mblnSuccess

End Function

Public Function EnumCallback(ByVal app_hWnd As Long, _
ByVal param As Long) As Long

Dim buf As String * 256
Dim title As String
Dim length As Long

' Checks a returned task to determine if App should be closed

' get window's title.
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)

' determine if target window.
If InStr(UCase(title), UCase(mstrTarget)) <> 0 Then
' Kill window.
SendMessage app_hWnd, WM_CLOSE, 0, 0
mblnSuccess = True
End If

' continue searching.
EnumCallback = 1

End Function

 

your loop should be adjusted (added the code in red):

For i = 1 To att.Count
fn = sPath & att(i).DisplayName
att(i).SaveAsFile fn
Next i
End If
If blnFindWindow("MESSAGE (HTML)") Then
If Not blnCloseWindow("MESSAGE (HTML)") Then
MsgBox "Problems encountered closing Window", _
vbInformation, "API Call"
End If
End If
sFile = Dir$()
Loop

Avatar
Marsil Sarvis
Member
Members
Level 0
Forum Posts: 73
Member Since:
September 6, 2019
sp_UserOfflineSmall Offline
9
September 24, 2021 - 2:03 am
sp_Permalink sp_Print

You are amazing! Thank you so much!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Andy Kirby, Scott Miller, Roy Lutke, Jeff Krueger, Kylara Papenfuss, Ivica Cvetkovski
Guest(s) 8
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.