7.7. E-mails and Outlook
One way you can send an e-mail from VBA code is using the SendObject method, as shown here:
DoCmd.SendObject ObjectType, ObjectName, OutputFormat, To, Cc, Bcc, Subject, MessageText, EditMessage, TemplateFile
The ObjectType, ObjectName, and OutputFormat parameters are used to specify a file created from the database to include as an attachment. Remember that earlier I said you had exported the tblEmployees table to Excel so that you could e-mail it to a coworker. The SendObject method allows you to attach certain database objects in one of a variety of formats as part of the e-mail. Thus, to generate a new e-mail that also attaches the tblEmployees table as an Excel attachment, you could use something similar to the following:
'Send the Employees file
DoCmd.SendObject acSendTable, "tblEmployees", acFormatXLS, _
"someone@yahoo.com", , , "Employee List", "For your review.", False
If you do not want to send an attachment, but just want to send an e-mail telling me how much you like the book so far, you can use the following command. Please do this—I would love to get this test e-mail from you!
'Send the author of this book an email
DoCmd.SendObject acSendNoObject, , , "denisegosnell@yahoo.com", , , _
"This is cool!", _
"I just sent an email from VBA. Really am enjoying your book.", False
If you want to learn more about controlling Outlook from your VBA applications, consult Chapter 11, where I cover automation with various Office programs such as Outlook. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access