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:
153311 (http://support.microsoft.com/kb/153311/EN-US/)
Using Automation to Send a Microsoft Exchange Message
Back to the top
NOTE: 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:
262631 (http://support.microsoft.com/kb/262631/EN-US/) OL2000: Information About the Outlook E-mail Security Update
262617 (http://support.microsoft.com/kb/262617/EN-US/) 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
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 |
Back to the top
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 (http://support.microsoft.com/kb/160502/EN-US/)
ACC: Using Automation to Add Appointments to Microsoft
Outlook
161012 (http://support.microsoft.com/kb/161012/EN-US/)
VBA: How to Create a New Contact Item in Outlook with
Automation
Back to the top