The main purpose of the following code is to Send a reminder as an email if TODAY + 60 > Deadline meaning that i need to get a reminder 2 months ahead of an expiring fixed contract of a vessel and take the names of the vessels that make my formula TRUE as 'Send Reminder' and forward only the names of the vessels on Column A to an email. I want this to run once a week but i need to make it run automatically when i open excel.
I made the following code to send an email via macro which works the way i want it. Not that much familiar with VBA.
I made the following code to send an email via macro which works the way i want it. Not that much familiar with VBA.
Code:
Sub SendReminderMail()
Dim OutApp As Object
Dim OutMail As Object
Dim no_vessels As Integer, vessel_name() As String, Formula() As String
Dim count As Integer, i As Integer, k As Integer, temp As Integer
Dim indicator() As Integer, names_to_send() As String
Dim rng As Range
Dim sCC As String, sSubj As String, sEmAdd As String
'// Change No_vessels to match excel count -1
no_vessels = 18
ReDim vessel_name(no_vessels, 1), Formula(no_vessels, 1)
For i = 2 To (no_vessels + 1)
vessel_name(i - 1, 1) = Cells(i, "A")
Formula(i - 1, 1) = Cells(i, "D")
Next i
count = 0
For i = 1 To no_vessels
If Formula(i, 1) = "Send Reminder" Then
count = count + 1
End If
Next i
k = 1
ReDim indicator(count, 1)
For i = 1 To no_vessels
If Formula(i, 1) = "Send Reminder" Then
indicator(k, 1) = i
k = k + 1
End If
Next i
ReDim names_to_send(count, 1)
For i = 1 To count
temp = indicator(i, 1)
names_to_send(i, 1) = vessel_name(temp, 1)
Next i
'// change for output cell for email
Range("F1").Activate
r = ActiveCell.Row
c = ActiveCell.Column
For i = 1 To count
Cells(i, c).Value = names_to_send(i, 1)
Next i
'// Change the values of these variables to suit
sEmAdd = "panos.kwstopoulos@gmail.com"
sCC = ""
sSubj = "ATTENTION on fixtures expiring"
Set rng = Nothing
On Error Resume Next
Set rng = Range("F1:F" & count)
On Error GoTo 0
With Application
.EnableEvents = 0
.ScreenUpdating = 0
.Calculation = xlCalculationManual
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = sEmAdd
.CC = sCC
.Subject = sSubj
.HTMLBody = "Dear 360, " & _
"Please find below list of vessels with expring fixtures" & _
RangetoHTML(rng) & _
"Thank you"
.Send '// Change this to .Display if you want to view the email before sending.
End With
On Error GoTo 0
With Application
.EnableEvents = 1
.Calculation = xlCalculationAutomatic
End With
Set OutMail = Nothing: Set OutApp = Nothing
End Sub