Advertisement
6_2008-2009 Microsoft Office Apps/VBA #207157

Outlook Automation w/ body formattting

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.)

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'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")
Original Comments (3)
Recovered from Wayback Machine