OnTime Method VBA Code

smartguy

Well-known Member
Joined
Jul 14, 2009
Messages
778
Hello all,

I have one Macro. I need to run macro automatically on daily 8.30 P.m

Please help....
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello Sir,

I am not sure.....

Where can i add the code?

Code:
Sub Mailer()

    Dim OutApp As Object, OutMail As Object
    Dim rngCell As Range
               
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    
    For Each rngCell In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
        If DateSerial(Year(Date), Month(rngCell.Value), Day(rngCell.Value)) = Date Then
            With OutMail
                .To = rngCell.Offset(0, 1).Value
                .CC = ""
                .BCC = ""
                .Subject = "Happy Birthday"
                .Body = "Blah Blah Blah"
                .Display
            End With
                            
            Set OutMail = Nothing
            Set OutApp = Nothing
                               
            With Application
                waitTime = TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1)
                .Wait waitTime
                .SendKeys "%s~~~"
            End With
        End If
    Next rngCell
                
End Sub
 
Upvote 0
Adjust that to

Rich (BB code):
Public dTime As Date

Sub Mailer()

    Dim OutApp As Object, OutMail As Object
    Dim rngCell As Range
dTime = Now + 1
Application.OnTime dTime, "Mailer"
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    
    For Each rngCell In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
        If DateSerial(Year(Date), Month(rngCell.Value), Day(rngCell.Value)) = Date Then
            With OutMail
                .To = rngCell.Offset(0, 1).Value
                .CC = ""
                .BCC = ""
                .Subject = "Happy Birthday"
                .Body = "Blah Blah Blah"
                .Display
            End With
                            
            Set OutMail = Nothing
            Set OutApp = Nothing
                               
            With Application
                waitTime = TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1)
                .Wait waitTime
                .SendKeys "%s~~~"
            End With
        End If
    Next rngCell
                
End Sub

Then in the ThisWorkbook module

Rich (BB code):
Private Sub Workbook_Open()

  Application.OnTime TimeValue("20:30:00"), "Mailer"

End Sub

Save, close and re-open the workbook.
 
Upvote 0
Thanks.

I tried I got a run time error 91.

Object variable or with Block Variable not set..

Code:
.To = rngCell.Offset(0, 1).Value
 
Upvote 0
The code compiles OK for me. Did it work OK when you ran it manually (not using OnTime).
 
Upvote 0
Does the code work correctly when you run the original code that you posted? If not then post back to the thread where you were given that code.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
Members
452,902
Latest member
Knuddeluff

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