Jyggalag
Active Member
- Joined
- Mar 8, 2021
- Messages
- 445
- Office Version
- 365
- 2019
- Platform
- Windows
Hi all,
I have the current VBA code:
However, when I try to run the code, it highlights my ws.range in red and says my code does not work.
Anybody know why?
Kind regards,
Jyggalag
I have the current VBA code:
VBA Code:
Option Explicit
Private Const FilePath As String = "S:\File Path anonymous\"
Sub send_email_complete()
Dim OutApp As Object
Dim OutMail As Object
Dim i As Long
Dim ws As Worksheet
Dim col As New Collection, itm As Variant
Dim ToAddress As String, CCAddress As String, EmailSubject As String
'~~> Change this to the relevant worksheet
'~~> that has the emails (right now Search Export has it)
Set ws = ThisWorkbook.Sheets("Search Export")
Set OutApp = CreateObject("Outlook.Application")
Dim BodyText As String
BodyText = ws.Range("G3") & "<BR>" & "<BR>" & _
ws.Range("G5") & "<BR>" & "<BR>" & _
ws.Range("G10") & "<BR>" & "<BR>" & _
ws.Range("G14") & "<BR>" & "<BR>" & _
ws.Range("G17") & "<BR>" & "<BR>" & _
ws.Range("G20") & "<BR>" & _
"<b>" ws.Range("G21") & & "</b><br>" & "<BR>" & _
ws.Range("G22") & "<BR>" & "<BR>" & _
ws.Range("G24") & "<BR>" & "<BR>" & _
ws.Range("G26") & "<BR>" & "<BR>" & _
ws.Range("G28") & "<BR>" & "<BR>" & _
ws.Range("G30") & "<BR>" & "<BR>" & _
ws.Range("G32") & "<BR>" & "<BR>" & _
ws.Range("G34") & "<BR>" & "<BR>" & _
ws.Range("G36") & "<BR>" & "<BR>" & _
ws.Range("G38") & "<BR>" & "<BR>" & _
Dim AttachmentName As String
AttachmentName = FilePath & ws.Cells(2, 6).Value2
For i = 2 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'~~> Constructing addresses and subject
ToAddress = ws.Cells(i, 2).Value2
CCAddress = ws.Cells(i, 5).Value2
EmailSubject = ws.Cells(i, 1).Value2
'~~> This creates a new email (so we can send out multiple emails)
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = ToAddress
.CC = CCAddress
.Subject = EmailSubject
.HTMLBody = BodyText
.Attachments.Add AttachmentName
.Display
End With
Next i
End Sub
However, when I try to run the code, it highlights my ws.range in red and says my code does not work.
Anybody know why?
Kind regards,
Jyggalag