Hello
I am trying to get VBA to create an email, add a 'To' address, a 'Cc' address, attach a file based on a file path and loop through a list to create an email for every row in a list for different people with unique files.
For example
<tbody>
</tbody>
I can't get the code to work through each line and create a new email.
Any help appreciated.
thanks
I am trying to get VBA to create an email, add a 'To' address, a 'Cc' address, attach a file based on a file path and loop through a list to create an email for every row in a list for different people with unique files.
For example
To | Cc | Bcc | File |
Director@company1.com | Manager@company1.com | C:/Documents/Company1File.pdf | |
Finance@company2.com | Finance2@company2.com | C:/Documents/Company2File.pdf | |
HR@company3.com | HRManager@company3.com | C:/Documents/Company3File.pdf | |
Legal@company4.com | Solicitor@Legal.com | C:/Documents/Company4File.pdf |
<tbody>
</tbody>
Code:
Sub SendFiles()
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
For Each cell In sh.Columns("B:C").Cells.SpecialCells(xlCellTypeConstants)
'Enter the path/file names in the D:Z column in each row
Set rng = sh.Cells(cell.Row, 1).Range("D1:Z1")
If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = Range("B1:").Value
.cc = Range("C1:").Value
.Subject = "Testfile"
.Body = "Hi " & vbNewLine & _
"this is a test" _
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Display
End With
Set OutMail = Nothing
End If
Next cell
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
I can't get the code to work through each line and create a new email.
Any help appreciated.
thanks