You are currently offline, waiting for your internet to reconnect

Using Automation to Send a Microsoft Outlook Message

This article was previously published under Q161088
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

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

This article assumes that you are familiar with Visual Basic forApplications and with creating Microsoft Access applications using theprogramming tools provided with Microsoft Access. For more informationabout Visual Basic for Applications, please refer to the "BuildingApplications with Microsoft Access 97" manual.

For more information about using Automation to send a Microsoft Exchangemessage, please see the following article in the Microsoft Knowledge Base:
153311Using Automation to Send a Microsoft Exchange Message
MORE INFORMATION
NOTE: The following code may not work properly if you haveinstalled the Outlook E-mail Security Update.For additional information about this update, please seeone of the following articles in the Microsoft KnowledgeBase, depending on which version of Outlook you have:
262631 OL2000: Information About the Outlook E-mail Security Update
262617 OL98: Information About the Outlook E-mail Security Update
The SendObject method provides a way to send a MAPI mail messageprogrammatically in Microsoft Access. However, the SendObject method doesnot give you access to complete mail functionality, such as the ability toattach an external file or set message importance. The example that followsuses Automation to create and send a mail message that you can use to takeadvantage of many features in Microsoft Outlook that are not available withthe SendObject method.

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

  • Initialize the Outlook session
  • Create a new message
  • Add the recipients (To, CC, and BCC) and resolve their names
  • Set valid properties, such as the Subject, Body, and Importance
  • Add attachments (if any)
  • Display/Send the message
To send a Microsoft Outlook mail message programmatically, follow thesesteps:

  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, searchthe Help Index for Automation, or ask the Microsoft Access 97 OfficeAssistant.

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
OutSol OutSol97 OutSol98
Properties

Article ID: 161088 - Last Review: 01/19/2007 20:50:42 - Revision: 3.3

  • Microsoft Access 97 Standard Edition
  • kbhowto kbinterop KB161088
Feedback