VBA Send Outlook Email

/ Published in: Visual Basic
Save to your folder(s)

To test this procedure, type the following line in the Debug window, and then press ENTER.

SendMessage True, "C:\My Documents\Customers.txt"

Note that a new message is displayed in Microsoft Outlook with an attachment.

To send the message without displaying it in Microsoft Outlook, call the procedure with a False value for the first argument:

SendMessage False, "C:\My Documents\Customers.txt"

To send the message without specifying an attachment, omit the second argument when calling the procedure.

SendMessage True

For more information about using Automation in Microsoft Access, search the Help Index for Automation, or ask the Microsoft Access 97 Office Assistant.

For more information about using Automation to control Microsoft Outlook, please see the following articles in the Microsoft Knowledge Base:

160502 ACC: Using Automation to Add Appointments to Microsoft Outlook

161012 VBA: How to Create a New Contact Item in Outlook with Automation

Copy this code and paste it in your HTML
  1. Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
  2. Dim objOutlook As Outlook.Application
  3. Dim objOutlookMsg As Outlook.MailItem
  4. Dim objOutlookRecip As Outlook.Recipient
  5. Dim objOutlookAttach As Outlook.Attachment
  7. ' Create the Outlook session.
  8. Set objOutlook = CreateObject("Outlook.Application")
  10. ' Create the message.
  11. Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  13. With objOutlookMsg
  14. ' Add the To recipient(s) to the message.
  15. Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
  16. objOutlookRecip.Type = olTo
  18. ' Add the CC recipient(s) to the message.
  19. Set objOutlookRecip = .Recipients.Add("Michael Suyama")
  20. objOutlookRecip.Type = olCC
  22. ' Add the BCC recipient(s) to the message.
  23. Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
  24. objOutlookRecip.Type = olBCC
  26. ' Set the Subject, Body, and Importance of the message.
  27. .Subject = "This is an Automation test with Microsoft Outlook"
  28. .Body = "This is the body of the message." &vbCrLf & vbCrLf
  29. .Importance = olImportanceHigh 'High importance
  31. ' Add attachments to the message.
  32. If Not IsMissing(AttachmentPath) Then
  33. Set objOutlookAttach = .Attachments.Add(AttachmentPath)
  34. End If
  36. ' Resolve each Recipient's name.
  37. For Each ObjOutlookRecip In .Recipients
  38. objOutlookRecip.Resolve
  39. Next
  41. ' Should we display the message before sending?
  42. If DisplayMsg Then
  43. .Display
  44. Else
  45. .Save
  46. .Send
  47. End If
  48. End With
  49. Set objOutlook = Nothing
  50. End Sub

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.