VB icon

Outlook Automation w/ body formattting

Email
Submitted on: 1/22/2015 1:55:00 PM
By: Jason Allen (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: VB 5.0, VB 6.0, VBA MS Access, VBA MS Excel
Views: 985
 
     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.)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Outlook Automation w/ body formattting
' Description: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.)
' By: Jason Allen (from psc cd)
'
' 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")


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.