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:
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
- 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:Option Explicit
- 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 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
For more information about using Automation to control Microsoft Outlook, please see the following articles in the Microsoft Knowledge Base:
ID článku: 161088 – Posledná kontrola: 19. 1. 2007 – Revízia: 1