Hi DKenny,

I am referring to this after a long-long time.
I tried this code of yours - it worked great for picking up one .msg file from a folder.
But the user has to run the macro multiple times to extract attachments from all the .msg files in a given folder.
Can you help pls with a modified version of this code, to open all the .msg files from a given folder and extract all the attachments (from each .msg) into another specified folder?
This was perhaps the original requirement of Shootme too!
May be Shootme could manage with the piece of code you shared.

Would be greatfull if you can enhance this code of yours to accomplish the task of extracting the attachments from each and every .msg files stored in a folder in one go (macro to be executed only once).

Regards

Rajesh


Quote Originally Posted by DKenny View Post
Opening MailItems that have been saved as .msg files is really difficult. One easy work around it to create a copy of the file using the CreateItemFromTemplate method of the Outlook application. This makes a copy of the msg file that you can work with in subsequent code.
Here's a procedure that should give you what you need. I've used the FileDialog object rather than the GetOpenFilename method, as it has move flexibility.
Note: I wrote this in Excel, and you will need to add a reference to the Outlook library.

VB Code:
  1. Option Explicit
  2.  
  3. Sub ShootMe()
  4. Dim sMessagePath As String
  5. Dim sSavePath As String
  6. Dim OLApp As Outlook.Application
  7. Dim oMessage As Outlook.MailItem
  8. Dim oMsgAttach As Outlook.Attachment
  9.  
  10.     '----------------------------------------------------------
  11.     'First get the path for the .msg file
  12.     '----------------------------------------------------------
  13.    
  14.     With Application.FileDialog(msoFileDialogFilePicker)
  15.        
  16.         'only allow a single file to be selected
  17.         .AllowMultiSelect = False
  18.        
  19.         .Title = "Select Message File"
  20.        
  21.         'Filter the file list to only
  22.         'include .msg files
  23.         With .Filters
  24.             .Clear
  25.             .Add "Message Files", "*.msg"
  26.         End With
  27.        
  28.         'If the user selects a file
  29.         'record the path,
  30.         'otherwise quit execution
  31.         If .Show = -1 Then
  32.             sMessagePath = .SelectedItems(1)
  33.         Else
  34.             Exit Sub
  35.         End If
  36.     End With
  37.    
  38.     '----------------------------------------------------------
  39.     'Next, get the save path
  40.     '----------------------------------------------------------
  41.    
  42.     With Application.FileDialog(msoFileDialogFolderPicker)
  43.         .AllowMultiSelect = False
  44.         .Title = "Select Save Folder"
  45.        
  46.         If .Show = -1 Then
  47.             sSavePath = .SelectedItems(1)
  48.         Else
  49.             Exit Sub
  50.         End If
  51.     End With
  52.    
  53.     '----------------------------------------------------------
  54.     'Now we need to open the message and export the attachments
  55.     '----------------------------------------------------------
  56.    
  57.     'Create Outlook objects
  58.     Set OLApp = New Outlook.Application
  59.     Set oMessage = OLApp.CreateItemFromTemplate(sMessagePath)
  60.    
  61.     'Loop through each attachment...
  62.     For Each oMsgAttach In oMessage.Attachments
  63.    
  64.         With oMsgAttach
  65.             '... saving it to the destination folder
  66.             .SaveAsFile Path:=sSavePath & "\" & .DisplayName
  67.         End With
  68.     Next oMsgAttach
  69.    
  70.     'Clear object variables
  71.     Set OLApp = Nothing
  72.     Set oMessage = Nothing
  73.     Set oMsgAttach = Nothing
  74. End Sub