You can try the code that is given below for the attaching of the various files and also the header details and workbook information by just using the code that is given below:
Code:
ub Sort_By_Make()
On Error Resume Next
Dim objOutlook As Object
Dim Mail As Object
vName = ActiveWorkbook.Name
vSheet = ActiveSheet.Name
Columns("A:E").Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
vStart = i
Do Until Workbooks(vName).Sheets(vSheet).Range("D" & i).Value <> Range("D" & i + 1).Value
i = i + 1
Loop
Workbooks.Add
vNewBook = ActiveWorkbook.Name
Workbooks(vName).Sheets(vSheet).Range("A1:E1").Copy Destination:=Workbooks(vNewBook).Sheets("Sheet1").Range("A1")
Workbooks(vName).Sheets(vSheet).Range("A" & vStart & ":E" & i).Copy Destination:=Workbooks(vNewBook).Sheets("Sheet1").Range("A2")
Cells.AutoFilter
Columns.EntireColumn.AutoFit
vPath = "C:\Test\" & Replace(Date, "/", "_") & " " & Workbooks(vName).Sheets(vSheet).Range("D" & i).Value & " 3rd Follow-up " & Replace(Time, ":", "_") & ".xlsm"
ActiveWorkbook.SaveAs (vPath)
vNewName = ActiveWorkbook.Name
Workbooks(vNewName).Close
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With objMail
.Subject = Replace(vNewName, ".xlsm", "")
For j = 3 To Sheets("Email Addresses").Cells((Application.WorksheetFunction.Match((Workbooks(vName).Sheets(vSheet).Range("D" & i).Value), Sheets("Email Addresses").Range("A:A"), 0)), Columns.Count).End(xlToLeft).Column
If Err.Number = 1004 Then
For Each vTab In Sheets
If vTab.Name = "Errors" Then
vFound = True
End If
Next vTab
If vFound <> True Then
Sheets.Add.Name = "Errors"
Sheets("Errors").Range("A1").Value = "NOT FOUND"
End If
Sheets("Errors").Range("A" & Sheets("Errors").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = Workbooks(vName).Sheets(vSheet).Range("D" & i).Value
Err.Clear
GoTo Skip
End If
vNames = Workbooks(vName).Sheets("Email Addresses").Cells(Application.WorksheetFunction.Match(Workbooks(vName).Sheets(vSheet).Range("D" & i).Value, Workbooks(vName).Sheets("Email Addresses").Range("A:A"), 0), j).Value
.Recipients.Add (vNames)
Next j
.attachments.Add vPath
.Body = Workbooks(vName).Sheets("Email Addresses").Range("B" & Application.WorksheetFunction.Match(Workbooks(vName).Sheets(vSheet).Range("D" & i).Value, Workbooks(vName).Sheets("Email Addresses").Range("A:A"), 0)).Value
End With
objMail.display
'objMail.Send
Skip:
Set objMail = Nothing
Set objOutlook = Nothing
Next i
End Sub
And see that whether it is working out for you or not.
Bookmarks