title: Save, Open and Sort Excel attachments from a macro in Outlook
body: <p>I receive reports via outlook every 15 minutes. These reports actually come in Excel      attachments. I only work 8 hours a day; the next day I usually have a build up of reports that have come every 15 minutes since the previous day.  I have to then open each report individually and then sort them by header. </p>
  
  <p>I know how to open and save each unread email attachment and save it on to my computer: </p>
  
  <pre><code>         Sub GetAttachments()
               On Error GoTo GetAttachments_err
               Dim ns As NameSpace
               Dim Inbox As MAPIFolder
               Dim SubFolder As MAPIFolder
               Dim Item As Object
               Dim Atmt As Attachment
               Dim FileName As String
               Dim i As Integer
  
               Set ns = GetNamespace("MAPI")
               Set Inbox = ns.GetDefaultFolder(olFolderInbox)
               Set Mailbox = Inbox.Parent
               Set SubFolder = Mailbox.Folders("Local Archive")
               i = 0
  
               'check if there is any mail in the folder'
                If SubFolder.Items.Count = 0 Then
                MsgBox "There are no messages in the folder.", vbInformation, _
                   "Nothing Found"
                 Exit Sub
                 End If
  
            'Check each message and save the attachment'
              If SubFolder.Items.Count &gt; 0 Then
                For Each Item In SubFolder.Items
                   If Item.UnRead = True Then
                       For Each Atmt In Item.Attachments
                        FileName = "C:\Users\badana\Desktop\" &amp; Atmt.FileName
                        Atmt.SaveAsFile FileName 'saves each attachment'
  
                        'this code opens each attachment'
                         Set myShell = CreateObject("WScript.Shell")
                         myShell.Run FileName
  
                         'this sets the email as read'
                          Item.UnRead = False
                         'updates the counter'
                         i = i + 1
  
                          Next Atmt
                    End If
                Next Item
              End If
  
    'Display results
  
    If i &gt; 0 Then
     MsgBox "I found " &amp; i &amp; " attached files." _
     &amp; vbCrLf &amp; "They are saved on your desktop" _
     &amp; vbCrLf &amp; vbCrLf &amp; "Have a nice day.", vbInformation, "Finished!"
     Else
   MsgBox "I didn't find any attached files in your mail.", vbInformation, _
   "Finished!"
   End If
  
         'Replenish Memory'
          GetAttachments_exit:
  
         Set Atmt = Nothing
         Set Item = Nothing
         Set ns = Nothing
          Exit Sub
  
   'function for sorting the excel attachment'
  
  
    GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
    &amp; vbCrLf &amp; "Please note and report the following information." _
    &amp; vbCrLf &amp; "Macro Name: GetAttachments" _
    &amp; vbCrLf &amp; "Error Number: " &amp; Err.Number _
    &amp; vbCrLf &amp; "Error Description: " &amp; Err.Description _
    , vbCritical, "Error!"
      Resume GetAttachments_exit
   End Sub
  </code></pre>
  
  <p>I have the code for sorting each attachment by header:</p>
  
  <pre><code>Sub SortData()
  '
  ' SortData Macro
  ' sorts data
  '
  
  '
    Dim lngLast As Long
    lngLast = Range("A" &amp; Rows.Count).End(xlUp).Row
  
    Cells.Select
  ActiveWorkbook.Worksheets("02APR14").Sort.SortFields.Clear
  ActiveWorkbook.Worksheets("02APR14").Sort.SortFields.Add Key:=Range("A2:A" &amp; lngLast) _
      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  ActiveWorkbook.Worksheets("02APR14").Sort.SortFields.Add Key:=Range("K2:K" &amp; lngLast) _
      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  With ActiveWorkbook.Worksheets("02APR14").Sort
      .SetRange Range("A1:L" &amp; lngLast)
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
   End Sub
  </code></pre>
  
  <p>However, I want to be able to combine both codes in outlook such that I can run it as          one macro from outlook, it will open and save every attachment and also sort them in one go. is this possible? </p>