vba code for email

sgolder

Board Regular
Joined
Oct 31, 2002
Messages
151
I would like to set a spreadsheet to open from outlook and email to a specific person at a certain time on a monday morning. does anyone know the code for this?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
any reason why you can't do this from XL? (Why OL specifically...though yes, you can do it).
 
Upvote 0
eg OL Code...set as module in Outlook VBA

Sub mail_book()

If Hour(Now()) = 10 Then

Dim aOutlook As Outlook.Application, aEmail As Outlook.MailItem

Set aOutlook = GetObject(, "Outlook.Application")

Set aEmail = aOutlook.CreateItem(olMailItem)

aEmail.Importance = olImportanceHigh

aEmail.Subject = "First ABT Reminder For - Dated " & Today
aEmail.Body = "This is a Test"
aEmail.Recipients.Add "j.logss@loggsville.com"
aEmail.Attachments.Add ("C:\MrE\1.xls")

On Error GoTo ErrorHandler
aEmail.Send

Exit Sub

ErrorHandler:

Set aEmail = aOutlook.CreateItem(olMailItem)
aEmail.Importance = olImportanceHigh
aEmail.Subject = "ABT Alert Error For Ee - Dated " & Date
aEmail.Recipients.Add "j.bloggs@bloggs.com"
aEmail.Send
Resume Next

End If

End Sub
 
Upvote 0
in VB Editor you need to reference the Outlook object library - Tools - References - Outlook 9.0 / 10.0 object library pending your XL version.
 
Upvote 0
how do i specify an exact time to send the email?

and how do i get it to run at that time.?
 
Upvote 0
OK well there are quite a few messages here about scheduling in XL - the main approach within XL is to use the OnTime Method - however this requires that the file is open in order for the OnTime to kick in...ie. the macro will execute at a certain time.

My own preference is to use a Windows Scheduler (downloadable for free off the net) which will launch the Excel Application at a specific time (ie whe you want the email sent) , and then open a specified file - I then use a Workbook_Open event in the file - and as part of that code include some tests to determine as to whether or not I want the macro to execute fully though in this instance it's probably not necessary as you just get the scheduler to execute when you do want the mail sent....

eg

Private Sub Workbook_Open()

your code...

ActiveWorkbook.Close SaveChanges = False 'will save

End Sub

I am no expert with On Time so suggest you look around if you'd prefer that approach...I just prefer to not have files open all the time when it can be avoided.
 
Upvote 0

Forum statistics

Threads
1,221,695
Messages
6,161,360
Members
451,699
Latest member
sfairbro

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