Return to Snippet

Revision: 9843
at November 25, 2008 15:04 by fostersolutions


Initial Code
Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
          Dim objOutlook As Outlook.Application
          Dim objOutlookMsg As Outlook.MailItem
          Dim objOutlookRecip As Outlook.Recipient
          Dim objOutlookAttach As Outlook.Attachment

          ' Create the Outlook session.
          Set objOutlook = CreateObject("Outlook.Application")

          ' Create the message.
          Set objOutlookMsg  = objOutlook.CreateItem(olMailItem)

          With objOutlookMsg
              ' Add the To recipient(s) to the message.
              Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
              objOutlookRecip.Type = olTo

              ' Add the CC recipient(s) to the message.
              Set objOutlookRecip = .Recipients.Add("Michael Suyama")
              objOutlookRecip.Type = olCC

             ' Add the BCC recipient(s) to the message.
              Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
              objOutlookRecip.Type = olBCC

             ' Set the Subject, Body, and Importance of the message.
             .Subject = "This is an Automation test with Microsoft Outlook"
             .Body = "This is the body of the message." &vbCrLf & vbCrLf
             .Importance = olImportanceHigh  'High importance

             ' Add attachments to the message.
             If Not IsMissing(AttachmentPath) Then
                 Set objOutlookAttach = .Attachments.Add(AttachmentPath)
             End If

             ' Resolve each Recipient's name.
             For Each ObjOutlookRecip In .Recipients
                 objOutlookRecip.Resolve
             Next

             ' Should we display the message before sending?
             If DisplayMsg Then
                 .Display
             Else
                 .Save
                 .Send
             End If
          End With
          Set objOutlook = Nothing
      End Sub

Initial URL

                                

Initial Description
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

Initial Title
VBA Send Outlook Email

Initial Tags

                                

Initial Language
Visual Basic