PDA

View Full Version : Syntax for sending email



ntambomvu
10-16-2015, 01:46 AM
Hello Chaps-
(First time posting so please point out any mistakes)
I am new to VBA and am trying to put together some code to automate
some stuff on a excel 2013 worksheet

Could someone please give me an example of the correct syntax for
telling excel to send an email to an email address in a worksheet

Second thing - please recommend an entry level book or tutorial for a VBA Beginner

many thanks

fred

mancubus
10-16-2015, 02:33 AM
welcome to the forum.

amazon is your friend to find vba books.
you may like to try on-line traning, such as the one vba Express provided.
visit training at http://www.vbaexpress.com/excel-academy.html

below is a working code. you should amend it to meet your requirements.


Sub vbax_54013_email_from_excel_sample()

Dim fPath As String, fName As String, MailHtmlBody As String
fPath = "C:\ExampleFolder\"
fName = "ExampleFile.xlsx"

MailHtmlBody = "<font size=""3"" face=""Calibri"">Hi, <br><br>" & _
"Please find attached the file you requested.<br><br>" & _
"Cordially.</font>"

With CreateObject("Outlook.Application")
With .CreateItem(0)
.SentOnBehalfOfName = aa@xy.com 'if you are authorized to email on behalf of someone
.To = "bb@xy.com"
.CC = "cc@xy.com"
.BCC = "dd@xy.com"
.Subject = "Marketing report"
.Attachments.Add fPath & fName
.HTMLBody = MailHtmlBody
.ReadReceiptRequested = False
.Save
.Display
.Send
End With
End With
End Sub


[EMAIL = ...] ......[ /EMAIL ] tags are automatically inserted by forum. remove them in your code.