Hi Excel Experts,
I've managed to get the below VBA working from an old (2019) thread. It creates emails to recipients for each row in a spreadsheet, customizes the email content (with the recipients name) and also attaches a file based on a file path in column E of the sheet. However, it attaches all the files in the folder. What I need is to attach only the files base on the filename in Column F.
I am very new to VBA, tried to change the code of the filename, but no luck.
Thanking you in advance for any response.
Sub SendMail()
ActiveWorkbook.RefreshAll
Dim objOutlook As Object
Dim objMail As Object
Dim Attachments As Object
Dim ws As Worksheet
Dim fileName As String
Set objOutlook = CreateObject("Outlook.Application")
Set ws = ActiveSheet
For Each cell In ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
Set objMail = objOutlook.CreateItem(0)
With objMail
.To = cell.Value
.CC = cell.Offset(0, 1).Value
.Subject = cell.Offset(0, 2).Value
.Body = cell.Offset(0, 3).Value
fileName = Dir(cell.Offset(0, 4).Value & "\*")
While fileName <> vbNullString
.Attachments.Add cell.Offset(0, 4).Value & "\" & fileName
fileName = Dir()
Wend
.Display
End With
Set objMail = Nothing
Next cell
Set ws = Nothing
Set objOutlook = Nothing
End Sub
I've managed to get the below VBA working from an old (2019) thread. It creates emails to recipients for each row in a spreadsheet, customizes the email content (with the recipients name) and also attaches a file based on a file path in column E of the sheet. However, it attaches all the files in the folder. What I need is to attach only the files base on the filename in Column F.
I am very new to VBA, tried to change the code of the filename, but no luck.
Thanking you in advance for any response.
Sub SendMail()
ActiveWorkbook.RefreshAll
Dim objOutlook As Object
Dim objMail As Object
Dim Attachments As Object
Dim ws As Worksheet
Dim fileName As String
Set objOutlook = CreateObject("Outlook.Application")
Set ws = ActiveSheet
For Each cell In ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
Set objMail = objOutlook.CreateItem(0)
With objMail
.To = cell.Value
.CC = cell.Offset(0, 1).Value
.Subject = cell.Offset(0, 2).Value
.Body = cell.Offset(0, 3).Value
fileName = Dir(cell.Offset(0, 4).Value & "\*")
While fileName <> vbNullString
.Attachments.Add cell.Offset(0, 4).Value & "\" & fileName
fileName = Dir()
Wend
.Display
End With
Set objMail = Nothing
Next cell
Set ws = Nothing
Set objOutlook = Nothing
End Sub