Consulting

Results 1 to 5 of 5

Thread: Code for sending mail on specific time and day

  1. #1
    VBAX Contributor
    Joined
    Jul 2017
    Posts
    110
    Location

    Code for sending mail on specific time and day

    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

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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/Outlo...-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/Outlo...-event-outlook
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    VBAX Contributor
    Joined
    Jul 2017
    Posts
    110
    Location
    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

  4. #4

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Quote Originally Posted by Veeru View Post
    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.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •