This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
IN THIS TASK
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
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:
There are six main steps to sending a Microsoft Outlook mail message by using Automation, as follows:
- 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.
- Create a sample text file named Customers.txt in the C:\My Documents folder.
- 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 to select the Microsoft Outlook 9.0 Object Library, and then click OK.
NOTE: If the Microsoft Outlook 9.0 Object Library does not appear in the Available References box, browse your hard disk for the file, Msoutl9.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(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("Andrew Fuller")
objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "Last test - I promise." & 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
If Not objOutlookRecip.Resolve Then
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
- To test this procedure, type the following line in the Immediate window, and then press ENTER: To send the message without specifying an attachment, omit the argument when calling the procedure, as follows:
SendMessage "C:\My Documents\Customers.txt"