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:
Using Automation to Send a Microsoft Exchange Message
The following code may not work properly if you have
installed the Outlook E-mail Security Update.
For additional information about this update, please see
one of the following articles in the Microsoft Knowledge
Base, depending on which version of Outlook you have:
OL2000: Information About the Outlook E-mail Security Update
OL98: Information About the Outlook E-mail Security Update
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:
- 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 these
- Create a sample text file named Customers.txt in the C:\My Documents
- Start Microsoft Access and open the sample database Northwind.mdb.
- Create a module and type the following line in the Declarations
section if it is not already there:
- On the Tools menu, click References.
- 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.
- 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)
' 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)
' Resolve each Recipient's name.
For Each ObjOutlookRecip In .Recipients
' Should we display the message before sending?
If DisplayMsg Then
Set objOutlook = Nothing
- 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
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.
For more information about using Automation in Microsoft Access, search
the Help Index for Automation
, or ask the Microsoft Access 97 Office
For more information about using Automation to control Microsoft Outlook,
please see the following articles in the Microsoft Knowledge Base:
ACC: Using Automation to Add Appointments to Microsoft
VBA: How to Create a New Contact Item in Outlook with
Article ID: 161088 - Last Review: January 19, 2007 - Revision: 3.3
- Microsoft Access 97 Standard Edition
|kbhowto kbinterop KB161088|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.