by Jason Allen (3 Submissions)
Category: Microsoft Office Apps/VBA
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (7 Votes)
Use VB to automate sending an email behind the scenes with Outlook. Allows text formatting of the body message (IE, bold, font size, color, italics, etc.)
Assumes
Assume you are using Microsoft Outlook for email
'This code shows how to use VB to automate creating an Outlook Email. The main point to this is
'to show how you can use automation and still format your email body with bolding, font colors, sizes, etc.
'You must have the Outlook reference library checked for this code to use. Goto
'References and select the Microsoft Outlook Object Library.
'strings that store your variables for the email
Dim mySubject As String
Dim myBody As String
Dim myText As String
Dim myEmail As String
'this is where you can enter your body message. You can add on later by appending with the & symbol
myBody = myBody & "You can use any HTML tags to format your body message as you want."
'your subject goes here
mySubject = "This is my subject"
'declare a new instance of Outlook
Dim myOutlook As New Outlook.Application
'set the variable as a new outlook app
Set myOutlook = New Outlook.Application
'set a variable as a new outlook mail item
Set myMessage = myOutlook.CreateItem(olMailItem)
'this copies your body variable into the 'body' of the email.
'note the HTML declaration, as this allows you to use bold, italic, etc. tags in your body with Automation.
'This is the reason I created this code sample, so you can see how to format an email body any way you want
'using automation (you could use font tags and set font colors and everything this way.)
myMessage.HTMLBody = myBody
'sends your email
myMessage.send
'puts your subject variable into the 'subject' field
myMessage.Subject = mySubject
'puts your email address variable into the 'to' field
myMessage.To = myEmail
'Empties the Variables from memory
Set myMessage = Nothing
Set myOutlook = Nothing
'confirmation dialog box
myResult = MsgBox("Your email has been sent.", vbOKOnly, "Email Sent")