1

I receive a set of emails every day with an msg attachment.

I need to:

  1. Open the msg file and extract the sender name and msg received time;
  2. Save the msg attachment with file name e.g., ReceivedTime(ddmmyyyy)_SenderName_AttachmentName

I’ve code, from here, to find the email and save the attachment in a destination folder.

How do I extract the info inside the msg attachment?

Sub SaveOlAttach_Email()
    
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
    
    Dim isAttachment As Boolean
    Dim olFolder As Outlook.MAPIFolder
    Dim msg As Outlook.MailItem
    Dim att As Outlook.Attachment
    Dim fsSaveFolder, sSavePathFS, ssender As String
    
    fsSaveFolder = "C:\Users\xxx"
    isAttachment = False
    Set olFolder = Outlook.GetNamespace("MAPI").Folders("yyy")
    Set olFolder = olFolder.Folders("Inbox")
    If olFolder Is Nothing Then Exit Sub
    
    For Each msg In olFolder.Items
        If msg.Subject = "zzz" Then
            If msg.Attachments.Count > 0 Then
                While msg.Attachments.Count > 0
              
                    sSavePathFS = fsSaveFolder & Format(olFolder.Items(1).ReceivedTime, "yyyymmdd") & ".msg"
                    msg.Attachments(1).SaveAsFile sSavePathFS
                
                    msg.Attachments(1).Delete
                
                    isAttachment = True
                
                Wend
                msg.Delete
            End If
        End If
    Next
    
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    
End Sub
Community
  • 1
  • 1

0 Answers0