Public Sub Extract_Attachments_From_Outlook_Msg_Files()
Dim outApp As Object
Dim outEmail As Object
Dim outAttachment As Object
Dim msgFiles As String, sourceFolder As String, saveInFolder As String
Dim fileName As String
msgFiles = "C:\path\to\folder\*.msg" 'CHANGE - folder location and filespec of .msg files
saveInFolder = "C:\path\to\folder" 'CHANGE - folder where extracted attachments are saved
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
sourceFolder = Left(msgFiles, InStrRev(msgFiles, "\"))
On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
If outApp Is Nothing Then
MsgBox "Outlook is not open"
Exit Sub
End If
On Error GoTo 0
fileName = Dir(msgFiles)
While fileName <> vbNullString
'Open .msg file in Outlook 2003
'Set outEmail = outApp.CreateItemFromTemplate(sourceFolder & fileName)
'Open .msg file in Outlook 2007+
Set outEmail = outApp.Session.OpenSharedItem(sourceFolder & fileName)
For Each outAttachment In outEmail.Attachments
outAttachment.SaveAsFile saveInFolder & outAttachment.fileName
Next
fileName = Dir
Wend
End Sub
Dear Sir,
Dear Sir,
Still I getting error.
Pls help me this is very imp for me.
The code is for .msg (Outlook Message Format) files in a folder on your local disk, NOT for emails in an Outlook folder.My msg files are in outlook mail\inbox\msg folder and I want to extract on desktop.
Try this macro. You need to edit the code where indicated.
Code:Public Sub Extract_Attachments_From_Outlook_Msg_Files() Dim outApp As Object Dim outEmail As Object Dim outAttachment As Object Dim msgFiles As String, sourceFolder As String, saveInFolder As String Dim fileName As String msgFiles = "C:\path\to\folder\*.msg" 'CHANGE - folder location and filespec of .msg files saveInFolder = "C:\path\to\folder" 'CHANGE - folder where extracted attachments are saved If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\" sourceFolder = Left(msgFiles, InStrRev(msgFiles, "\")) On Error Resume Next Set outApp = GetObject(, "Outlook.Application") If outApp Is Nothing Then MsgBox "Outlook is not open" Exit Sub End If On Error GoTo 0 fileName = Dir(msgFiles) While fileName <> vbNullString 'Open .msg file in Outlook 2003 'Set outEmail = outApp.CreateItemFromTemplate(sourceFolder & fileName) 'Open .msg file in Outlook 2007+ Set outEmail = outApp.Session.OpenSharedItem(sourceFolder & fileName) For Each outAttachment In outEmail.Attachments outAttachment.SaveAsFile saveInFolder & outAttachment.fileName Next fileName = Dir Wend End Sub