VBA to extract all attachments from .msg file?

Alphix

New Member
Joined
Sep 29, 2016
Messages
22
:confused: I need help on how to extract attachments in from several .msg files in a designated folder.
I need a VBA code tode to do this.

Please help.:confused:
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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
 
Upvote 0
Dear Sir,

My msg files are in outlook mail\inbox\msg folder and I want to extract on desktop. According to your instructions but I am getting error "Expected: If or Else or Elself or End or Endif or Const". I hereby paste my macro below:
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 = "kiran.mirpuri@shankarpack.com\Inbox\MSG\*.msg"
saveInFolder = "D:\Tally.ERP9\Desktop\export"
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 = abc@xyz.com\Inbox\MSG

'Open .msg file in Outlook 2007+
Set outEmail =abc@xyz.com\Inbox\MSG

For Each outAttachment In outEmail.Attachments
outAttachment.SaveAsFile saveInFolder & outAttachment.fileName
Next

fileName = Dir

Wend

End Sub

PLEASE HELP ME OUT
 
Last edited by a moderator:
Upvote 0
Upvote 0
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

I created an account just to say "Thank you" for this. It saved me so much time!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top