Using Automation to Send a Microsoft Outlook Message

ID: Q161088

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to use Automation to create and send a Microsoft Outlook message in Microsoft Access 97.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access 97" manual.

For more information about using Automation to send a Microsoft Exchange message, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q153311
   TITLE     : Using Automation to Send a Microsoft Exchange Message

MORE INFORMATION

The SendObject method provides a way to send a MAPI mail message programmatically in Microsoft Access. However, the SendObject method does not give you access to complete mail functionality, such as the ability to attach an external file or set message importance. The example that follows uses Automation to create and send a mail message that you can use to take advantage of many features in Microsoft Outlook that are not available with the SendObject method.

There are six main steps when you use Automation to send a Microsoft Outlook mail message:

To send a Microsoft Outlook mail message programmatically, follow these steps:

1. Create a sample text file named Customers.txt in the C:\My Documents

   folder.

2. Start Microsoft Access and open the sample database Northwind.mdb.

3. Create a module and type the following line in the Declarations

   section if it is not already there:

      Option Explicit

4. On the Tools menu, click References.

5. In the References box, click the Microsoft Outlook 8.0 Object Model

   and then click OK.

   NOTE: If the Microsoft Outlook 8.0 Object Model does not appear in the
   Available References box, browse your hard drive for the file
   Msoutl8.olb. If you cannot locate this file, you must run the
   Microsoft Outlook Setup program to install it before you proceed
   with this example.

6. Type the following procedure in the new module:

      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

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

   ARTICLE-ID: Q160502
   TITLE     : ACC: Using Automation to Add Appointments to Microsoft
               Outlook

   ARTICLE-ID: Q161012
   TITLE     : VBA: How to Create a New Contact Item in Outlook with
               Automation

Additional query words: OutSol OutSol97 OutSol98
Keywords          : kbinterop kbole kbdta IntpOlea 
Version           : WINDOWS:97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: April 1, 1999