Hello everyone:
I can't figure out why my code when I use
it gives Runtime error Automation error Unspecified error.
What am in forgetting?
I can't figure out why my code when I use
VBA Code:
.Send
it gives Runtime error Automation error Unspecified error.
What am in forgetting?
VBA Code:
Option Explicit
Sub SendEmailAttachment()
Dim outlookApp As Object
Dim outMail As Object
Dim myAttachments As Object
Dim emailAddress As String
Dim emailAddressCC As String
Dim emailSubject As String
Dim fileName As String
Dim filePath As String
Dim attachment As String
Dim attachment2 As String
Dim signature As String
Dim lastrow As Integer
Dim x As Integer
x = 2
Do While Sheet1.Cells(x, 1) <> ""
Set outlookApp = CreateObject("Outlook.Application")
Set outMail = outlookApp.CreateItem(0)
Set myAttachments = outMail.Attachments
filePath = ThisWorkbook.Path & "\"
emailAddress = Sheet1.Cells(x, 3)
emailAddressCC = Sheet1.Cells(x, 13)
emailSubject = Sheet1.Cells(x, 17)
fileName = Sheet1.Cells(x, 16)
attachment = filePath + fileName
attachment2 = filePath + "CEO Letter to Employees.Pdf"
With outMail
.Send
End With
signature = outMail.HTMLbody
With outMail
.SentOnBehalfOfName = "sales@domain.com"
.To = emailAddress
.cc = emailAddressCC
.bcc = ""
.Subject = emailSubject
.HTMLbody = "Please find your statement attached" & vbCrLf & "Best Regards" & signature
myAttachments.Add (attachment2)
myAttachments.Add (attachment)
.Send
lastrow = lastrow + 1
emailAddress = ""
x = x + 1
Set outlookApp = Nothing
Set outMail = Nothing
End With
Loop
End Sub