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
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]To[/TD]
[TD]Cc[/TD]
[TD]Bcc[/TD]
[TD]File[/TD]
[/TR]
[TR]
[TD]Director@company1.com[/TD]
[TD]Manager@company1.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company1File.pdf[/TD]
[/TR]
[TR]
[TD]Finance@company2.com[/TD]
[TD]Finance2@company2.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company2File.pdf[/TD]
[/TR]
[TR]
[TD]HR@company3.com[/TD]
[TD]HRManager@company3.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company3File.pdf[/TD]
[/TR]
[TR]
[TD]Legal@company4.com[/TD]
[TD]Solicitor@Legal.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company4File.pdf[/TD]
[/TR]
</tbody>[/TABLE]
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
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]To[/TD]
[TD]Cc[/TD]
[TD]Bcc[/TD]
[TD]File[/TD]
[/TR]
[TR]
[TD]Director@company1.com[/TD]
[TD]Manager@company1.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company1File.pdf[/TD]
[/TR]
[TR]
[TD]Finance@company2.com[/TD]
[TD]Finance2@company2.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company2File.pdf[/TD]
[/TR]
[TR]
[TD]HR@company3.com[/TD]
[TD]HRManager@company3.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company3File.pdf[/TD]
[/TR]
[TR]
[TD]Legal@company4.com[/TD]
[TD]Solicitor@Legal.com[/TD]
[TD][/TD]
[TD]C:/Documents/Company4File.pdf[/TD]
[/TR]
</tbody>[/TABLE]
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