VBA, send mail before expiry date and do not send email if there are no items to be renewed

Yashika

New Member
Joined
Jul 13, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello, I have been working on a workbook where i am checking on a column to see if the items expiry date is less than 30 days and if yes, then i am sending notification emails. But, when there is no items to be renewed in 30 days the program is throwing error telling " Runtime Error -2147467259 (80004005) - We need to know who to send this to. Make sure you enter atleast one name". The code what i have written is as below:

Sub SendEmail()
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim MailDest As String

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)

With OutLookMailItem
MailDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(8))
If MailDest = "" And Cells(iCounter, 8).Offset(0, -1) = "Renew" Then
MailDest = Cells(iCounter, 8).Value
ElseIf MailDest <> "" And Cells(iCounter, 8).Offset(0, -1) = "Renew" Then
MailDest = MailDest & ";" & Cells(iCounter, 8)
End If
Next iCounter

.BCC = MailDest
.Subject = "FYI"
.Body = "Remainder mail to Renew. Please ignore if already Renewed"
.Send
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing
End Sub

Kindly let me know what is the mistake i have made so that the program runs fine even though there is no item to be renewed in my sheet.

K
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top