Posted By

fostersolutions on 11/25/08


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fostersolutions


VBA Send Outlook Email


 / Published in: Visual Basic
 

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

REFERENCES 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

  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
  6.  
  7. ' Create the Outlook session.
  8. Set objOutlook = CreateObject("Outlook.Application")
  9.  
  10. ' Create the message.
  11. Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  12.  
  13. With objOutlookMsg
  14. ' Add the To recipient(s) to the message.
  15. Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
  16. objOutlookRecip.Type = olTo
  17.  
  18. ' Add the CC recipient(s) to the message.
  19. Set objOutlookRecip = .Recipients.Add("Michael Suyama")
  20. objOutlookRecip.Type = olCC
  21.  
  22. ' Add the BCC recipient(s) to the message.
  23. Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
  24. objOutlookRecip.Type = olBCC
  25.  
  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
  30.  
  31. ' Add attachments to the message.
  32. If Not IsMissing(AttachmentPath) Then
  33. Set objOutlookAttach = .Attachments.Add(AttachmentPath)
  34. End If
  35.  
  36. ' Resolve each Recipient's name.
  37. For Each ObjOutlookRecip In .Recipients
  38. objOutlookRecip.Resolve
  39. Next
  40.  
  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  

Comments

RSS Icon Subscribe to comments
Posted By: vivekgaur on November 11, 2010

Code throws error at line " .display" Run-time error '-2147467259 (80004005)':

A dialog box is open. Close it and try again.

Please help

You need to login to post a comment.