Not getting daily auto email from excel VBA macro

SMS71

New Member
Joined
Jun 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am writing a macro to send an email daily at 9:30am from an excel by attaching the same excel. While i am able to get the mail when i test run the macro in VBA module window, but i am unable to get the mail daily automatically on the specified time. Below is my code.

*******ThisWorkbook Code********
Private Sub SetOnTime()
Dim Start As Date
Start = DateSerial(2021, 6, 14)
If Date <= Start + 5 Then
Call SendMail
Application.OnTime TimeValue("9:30:00"), "SendMail"
End If
End Sub

***********Module Code************
Public Const MailFunct = "SendMail"
Sub SendMail()
Dim outlookApp As Object
Dim myMail As Object

Set outlookApp = CreateObject("Outlook.Application")
Set myMail = outlookApp.CreateItem(olMailItem)

ThisWorkbook.Save
source_file = ThisWorkbook.FullName

With myMail
.To = "smadhu71@rediffmail.com"
.CC = "smadhu71@rediffmail.com"
.Subject = "Update"
.Body = " "
.Attachments.Add source_file
.Send
End With
End Sub
 

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.

SMS71

New Member
Joined
Jun 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,
Can someone pl help me in solving above code ?
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
4,211
This is the TIMER macro :

VBA Code:
Option Explicit

Dim TimeToRun

Sub auto_open()
    Call ScheduleEmail
End Sub

Sub ScheduleEmail()
    TimeToRun = TimeValue("09:30:00")
    Application.OnTime TimeToRun, "RunEmail"
End Sub

Sub RunEmail()
  
    Mail_workbook_Outlook
   
    Call ScheduleEmail
End Sub

Sub auto_close()
    On Error Resume Next
    Application.OnTime TimeToRun, "RunEmail", , False
End Sub


The I used my own email macro:


Code:
Option Explicit


Sub Mail_workbook_Outlook()


Dim OutApp As Object
Dim OutMail As Object
Dim sndEmail As String
Dim ccEmail As String
Dim bccEmail As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

    With OutMail
    .To = "smadhu71@rediffmail.com"
    .CC = "smadhu71@rediffmail.com"
    .BCC = ""
    .Subject = "Update"
    .Body = "This is the body"
    .Display
    End With

On Error GoTo 0


Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 
Solution

SMS71

New Member
Joined
Jun 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
This is the TIMER macro :

VBA Code:
Option Explicit

Dim TimeToRun

Sub auto_open()
    Call ScheduleEmail
End Sub

Sub ScheduleEmail()
    TimeToRun = TimeValue("09:30:00")
    Application.OnTime TimeToRun, "RunEmail"
End Sub

Sub RunEmail()
 
    Mail_workbook_Outlook
  
    Call ScheduleEmail
End Sub

Sub auto_close()
    On Error Resume Next
    Application.OnTime TimeToRun, "RunEmail", , False
End Sub


The I used my own email macro:


Code:
Option Explicit


Sub Mail_workbook_Outlook()


Dim OutApp As Object
Dim OutMail As Object
Dim sndEmail As String
Dim ccEmail As String
Dim bccEmail As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

    With OutMail
    .To = "smadhu71@rediffmail.com"
    .CC = "smadhu71@rediffmail.com"
    .BCC = ""
    .Subject = "Update"
    .Body = "This is the body"
    .Display
    End With

On Error GoTo 0


Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Hi,
Thanks for the code. I tried your code. The problem persist. The mail is not getting automatically pushed on specified time. I have to manually run the macro to push the mail. I want the mail to be automatically executed without manual intervention. Is this possible ?
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
4,211

ADVERTISEMENT

Do this ... delete the following lines from the macro :

VBA Code:
'Sub auto_open()
'    Call ScheduleEmail
'End Sub


In the ThisWorkbook module, paste this :

Code:
Option Explicit

Private Sub Workbook_Open()
    Call ScheduleEmail
End Sub

The macro should auto run when the workbook is opened.

I've had difficulty with the "auto_open" macro in the past. Don't understand why it would not function as designed but ... there were problems.
Sometimes it would run as designed and sometimes it would not.
 

SMS71

New Member
Joined
Jun 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,
Your inputs have resolved the issue to some extent. I think the mail has triggered but it has been held due to some other issue. Pl find attached the error msg. I thought the error may be related to enable / disable macro and i changed the Macro Settings to 'Enable All Macros' in Trust Center. But, still i did not receive the mail. Is there anything else missing ?
 

Attachments

  • Macro Error Msg.GIF
    Macro Error Msg.GIF
    38.6 KB · Views: 5

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
4,211

ADVERTISEMENT

Please post your workbook for download. This Forum does not provide a means of attaching a workbook. You will need to upload the workbook to a Cloud site such as DropBox.com or similar.

Be certain to remove any confidential information prior to posting.
 

SMS71

New Member
Joined
Jun 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Please post your workbook for download. This Forum does not provide a means of attaching a workbook. You will need to upload the workbook to a Cloud site such as DropBox.com or similar.

Be certain to remove any confidential information prior to posting.
Hi,
I have uploaded a sample blank file in DropBox.com. I may need a mail id to share.
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
4,211
Dropbox provides a link to the file you can share here.
 

Forum statistics

Threads
1,144,241
Messages
5,723,198
Members
422,482
Latest member
MacSapper

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
Top