Ive been working on email automation for excel using vba, my code works only for the first email, and got runtime error on the next,
ive tried setting object to nothing but to no avails..
i dont know where the leak in memory is
can somebody point me up
error is at item at object remark. I've provided a screenshot for the error..
Module Name = Automail
Public PublicRow As Integer
Dim mark As New Remarks
Sub Button_Click()
Dim LastR As Long
Dim CRow As Long
Dim sSendTo As String
Dim sSendCC As String
Dim sSendBCC As String
Dim sSubject As String
Dim txt As String
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set mark.item = Nothing
OutApp.Session.Logon
LastR = Cells(Rows.Count, 2).End(xlUp).Row
For CRow = 3 To LastR
If Cells(CRow, 6) <> "Email sent" Then
If Cells(CRow, 3) <= Date Then
Set OutMail = OutApp.CreateItem(0)
Set mark.item = OutMail
sSendTo = Cells(CRow, 5)
sSendCC = ""
sSendBCC = ""
sSubject = "Project Due Date"
PublicRow = CRow
With OutMail
.To = sSendTo
If sSendCC > "" Then .CC = sSendCC
If sSendBCC > "" Then .BCC = sSendBCC
.Subject = sSubject
txt = "Dear " & Cells(CRow, 4) & ", "
txt = txt & vbCrLf & vbCrLf
txt = txt & "The due date has been reached for the
project:"
txt = txt & vbCrLf & vbCrLf
txt = txt & " " & Cells(CRow, 2)
txt = txt & vbCrLf & vbCrLf
txt = txt & "Please take the appropriate actions."
txt = txt & vbCrLf & vbCrLf
txt = txt & "Regards,"
txt = txt & vbCrLf
txt = txt & "Danial"
.Body = txt
.Display (True)
End With
Set OutMail = Nothing
End If
End If
Next CRow
Set mark.item = Nothing
Set OutApp = Nothing
End Sub
Class Name = Remarks
Option Explicit
Public WithEvents item As Outlook.MailItem
Private Sub item_Close(Cancel As Boolean)
Dim boolSent As Boolean
boolSent = item.Sent
If Err.Number = 0 Then
Cells(PublicRow, 6) = "Email not sent"
Cells(PublicRow, 7) = "X"
Else
Cells(PublicRow, 6) = "Email sent"
Cells(PublicRow, 7) = Now()
End If
End Sub
Error: