Forum

Extract all attachm...
 
Notifications
Clear all

Extract all attachments from .EML file

9 Posts
2 Users
0 Reactions
404 Views
(@marsil)
Posts: 72
Estimable Member
Topic starter
 

Hello Team,

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

Thanks;

Marsil

 
Posted : 16/09/2021 6:56 pm
(@catalinb)
Posts: 1937
Member Admin
 

Try this code: (adapted from https://stackoverflow.com/questions/19255083/vba-outlook-extracting-attachments-from-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:UsersCatalinDesktop"

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

 
Posted : 20/09/2021 12:30 am
(@marsil)
Posts: 72
Estimable Member
Topic starter
 

Thank you Catalin.

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

issue-1.png

 
Posted : 20/09/2021 6:46 am
(@catalinb)
Posts: 1937
Member Admin
 

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

 
Posted : 20/09/2021 1:16 pm
(@marsil)
Posts: 72
Estimable Member
Topic starter
 

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

 
Posted : 21/09/2021 2:42 pm
(@catalinb)
Posts: 1937
Member Admin
(@marsil)
Posts: 72
Estimable Member
Topic starter
 

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

 
Posted : 22/09/2021 12:50 pm
(@catalinb)
Posts: 1937
Member Admin
 

Added this code in a module:

(adapted from http://www.pbdr.com/vbtips/api/FindCloseAPI.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

 
Posted : 23/09/2021 12:57 am
(@marsil)
Posts: 72
Estimable Member
Topic starter
 

You are amazing! Thank you so much!

 
Posted : 24/09/2021 12:03 pm
Share: