Hello,
I am new to VBA and have pieced together a code to send multiple emails. However, I am struggling with how I can add additional attachments (some of them are excel files and some are PDF files) to each of the emails. Any tips would be greatly appreciated. My code is below:
Sub Send_email_fromexcel()
Dim edress As String
Dim CC As String
Dim subj As String
Dim message As String
Dim filename As String
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myattachments As Object
Dim path As String
Dim Body As String
Dim lastrow As Integer
Dim attachment As String
Dim x As Integer
x = 2
Do While Sheet2.Cells(x, 1) <> ""
'For x = 2 To 4
Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.createitem(0)
Set myattachments = outlookmailitem.attachments
path = "C:\Users\mychal\Desktop"
edress = Sheet2.Cells(x, 1)
CC = Sheet2.Cells(x, 2)
subj = Sheet2.Cells(x, 3)
filename = Sheet2.Cells(x, 4)
Body = Sheet2.Cells(x, 5)
attachment = path + filename
outlookmailitem.To = edress
outlookmailitem.CC = CC
outlookmailitem.bcc = ""
outlookmailitem.Subject = subj
outlookmailitem.Body = Body
myattachments.Add (attachment)
outlookmailitem.display
lastrow = lastrow + 1
edress = ""
x = x + 1
Loop
Set outlookapp = Nothing
Set outlookmailitem = Nothing
End Sub
I am new to VBA and have pieced together a code to send multiple emails. However, I am struggling with how I can add additional attachments (some of them are excel files and some are PDF files) to each of the emails. Any tips would be greatly appreciated. My code is below:
Sub Send_email_fromexcel()
Dim edress As String
Dim CC As String
Dim subj As String
Dim message As String
Dim filename As String
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myattachments As Object
Dim path As String
Dim Body As String
Dim lastrow As Integer
Dim attachment As String
Dim x As Integer
x = 2
Do While Sheet2.Cells(x, 1) <> ""
'For x = 2 To 4
Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.createitem(0)
Set myattachments = outlookmailitem.attachments
path = "C:\Users\mychal\Desktop"
edress = Sheet2.Cells(x, 1)
CC = Sheet2.Cells(x, 2)
subj = Sheet2.Cells(x, 3)
filename = Sheet2.Cells(x, 4)
Body = Sheet2.Cells(x, 5)
attachment = path + filename
outlookmailitem.To = edress
outlookmailitem.CC = CC
outlookmailitem.bcc = ""
outlookmailitem.Subject = subj
outlookmailitem.Body = Body
myattachments.Add (attachment)
outlookmailitem.display
lastrow = lastrow + 1
edress = ""
x = x + 1
Loop
Set outlookapp = Nothing
Set outlookmailitem = Nothing
End Sub