PDA

View Full Version : Code for sending mail on specific time and day



Veeru
07-26-2017, 12:43 PM
Hi,

while working on one macro for outlook . I want to outlook to send automatic mail to team memebers on specific day and time..

Like every Monday at 4:00 pm .

I have one code which send mail on specific day but not regularly.

we need to enter a date and on reaching that day it will send mail.

Sub SendMail()
Dim OutApp As Object
Dim Outmail As Object
Dim strBody As String
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set Outmail = OutApp.CreateItem(0)
strBody = "Test"
On Error Resume Next
With Outmail
.To = "test@test.com" 'change "test@test.com" into "yourownmailadres@mail.com" to make the macro really work
.CC = ""
.BCC = ""
.Subject = "Test - No Reply - Automatic mail"
.HTMLBody = strBody
.Display
.DeferredDeliveryTime = 7 / 28 / 2017
End With
On Error GoTo 0
Set Outmail = Nothing
Set OutApp = Nothing
End Sub

Can anyone lookinto this and provide suggestion how we can modify it to send mail regularly every Monday at 4 pm.

thanks in advance

skatonni
07-26-2017, 02:43 PM
If Outlook is on at 4 pm then you could set up something with a reminder to run the code.

https://msdn.microsoft.com/VBA/Outlook-VBA/articles/application-reminder-event-outlook

A recurring appointment should be reasonable.

This code for ThisOutlookSession is the simplified version of what is shown in the link.

Untested but this is the idea.


Private Sub Application_Reminder(ByVal appt As Object)
If appt.Subject = "Test - SendMail / any unique appointment subject" then
SendMail
End If
End Sub



You can dismiss the reminder with the BeforeReminderShow event

https://msdn.microsoft.com/VBA/Outlook-VBA/articles/reminders-beforeremindershow-event-outlook

Veeru
07-27-2017, 07:26 AM
I m sorry I tried to understand what is it but not able to and I also tested your code,, but it didn't work....I need to send this mail every Monday at 4 pm so reminder will also not work.

Please any suggestion will be appreciated.
Thanks

Logit
07-27-2017, 08:07 AM
https://superuser.com/questions/635094/scheduled-and-recurring-email-in-outlook

https://www.extendoffice.com/documents/outlook/1567-outlook-send-schedule-recurring-email.html

skatonni
07-27-2017, 11:43 AM
I m sorry I tried to understand what is it but not able to and I also tested your code,, but it didn't work....I need to send this mail every Monday at 4 pm so reminder will also not work.

Please any suggestion will be appreciated.
Thanks

I tested and I believe the code should work as is. It is basically the same as the other suggested solution that uses a category to identify when to trigger SendMail.

Set up a recurring appointment, with a reminder. Use a unique subject, for example "Test - SendMail / any unique appointment subject". Put the code in the ThisOutlookSession module. Restart if you are now putting the code into ThisOutlookSession.

As indicated in my original post. Outlook has to be on at 4pm. If not, you will need more code to turn it on.