0% found this document useful (0 votes)
15 views4 pages

Excel Automatic Date Mail.

The document provides a VBA script for sending emails based on dates listed in an Excel worksheet. It includes steps to enable macros, ensure the macro runs on workbook open, and automate the process using Windows Task Scheduler. The final test confirms the functionality of the automated email sending process when the Excel file is opened.

Uploaded by

Himanshu Gaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Excel Automatic Date Mail.

The document provides a VBA script for sending emails based on dates listed in an Excel worksheet. It includes steps to enable macros, ensure the macro runs on workbook open, and automate the process using Windows Task Scheduler. The final test confirms the functionality of the automated email sending process when the Excel file is opened.

Uploaded by

Himanshu Gaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

VBA Alt+F11

Sub SendEmailsForAllRows()
Dim OutApp As Object
Dim OutMail As Object
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim sendDate As Date
Dim todayDate As Date
Dim recipient As String
Dim subject As String
Dim body As String

' Set the worksheet where email details are stored


Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" if
needed

' Get the last used row in column A


lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Get today's date


todayDate = Date

' Create Outlook application object (only once for efficiency)


Set OutApp = CreateObject("Outlook.Application")

' Loop through each row in column A


For i = 2 To lastRow ' Assuming row 1 is headers
sendDate = ws.Cells(i, 1).Value ' Column A (dates)
recipient = ws.Cells(i, 2).Value ' Column B (emails)
subject = ws.Cells(i, 3).Value ' Column C (subject)
body = ws.Cells(i, 4).Value ' Column D (email body)
' Check if today's date matches the send date
If sendDate = todayDate And recipient <> "" Then
' Create a new Outlook email
Set OutMail = OutApp.CreateItem(0)

' Configure email


With OutMail
.To = recipient
.Subject = subject
.Body = body
.Send ' Send email (use .Display to preview before sending)
End With

' Cleanup
Set OutMail = Nothing
End If
Next i

' Final cleanup


Set OutApp = Nothing
End Sub

Step 1: Ensure Macros Are Enabled


1. Open Excel.
2. Click File > Options.
3. Go to Trust Center > Trust Center Settings.
4. Under Macro Settings, select:
o ✅ Enable VBA macros.

o ✅ Trust access to the VBA project object model.

5. Click OK and restart Excel.

Step 2: Ensure the Macro Runs on Workbook Open


1. Press ALT + F11 to open VBA Editor.
2. In the Project Explorer, find ThisWorkbook under your Excel file.
3. Double-click ThisWorkbook and paste this code:
vba
CopyEdit
Private Sub Workbook_Open()
Call SendEmailsForAllRows
End Sub
4. Save the file as Macro-Enabled Workbook (.xlsm).

Step 3: Check If Excel Is Running Macros at Startup


 Close and reopen the file to test.
 If it doesn’t run, manually run the macro once:
1. Press ALT + F8.
2. Select SendEmailsForAllRows.
3. Click Run.
This initializes Excel’s macro permissions.

Step 4: Use Windows Task Scheduler (Fully Automatic)


If you want the macro to run without opening the file manually, schedule it
with Task Scheduler:
1. Create a New Task
1. Press Windows + R, type taskschd.msc, and press Enter.
2. Click Create Basic Task.
3. Name it "Auto Email from Excel" and click Next.
2. Set the Trigger
 Choose Daily or At startup.
 Set a start date and time.
3. Set the Action
 Select Start a Program.
 In the Program/Script box, browse to:
makefile
CopyEdit
C:\Program Files\Microsoft Office\root\OfficeXX\EXCEL.EXE
(Replace XX with your Office version, e.g., Office16 for Office 2016.)
 In the Add arguments box, enter:
arduino
CopyEdit
"C:\Path\To\Your\File.xlsm"
(Replace with the actual Excel file path.)
4. Save and Test
 Click Finish.
 Right-click the task and select Run.
This will open Excel and trigger the macro automatically.

Final Test
✅ Close Excel and reopen it.
✅ If it works, emails will send automatically when the file opens.
✅ If not, check macro settings again.
Let me know if you need more help! 🚀
4o

You might also like