Microsoft KB Archive/153311: Difference between revisions
(importing KB archive) |
m (Text replacement - "&" to "&") |
||
(One intermediate revision by the same user not shown) | |||
Line 54: | Line 54: | ||
This article describes how you can use Automation to create and to send a Microsoft Exchange message.<br /> | This article describes how you can use Automation to create and to send a Microsoft Exchange message.<br /> | ||
<br /> | <br /> | ||
This article assumes that you are familiar with Visual Basic for Applications and that you can create Microsoft Access applications by using the programming tools that are provided with Microsoft Access. For more information about Visual Basic for Applications, see your version of the | This article assumes that you are familiar with Visual Basic for Applications and that you can create Microsoft Access applications by using the programming tools that are provided with Microsoft Access. For more information about Visual Basic for Applications, see your version of the "Building Applications with Microsoft Access" manual.<br /> | ||
<br /> | <br /> | ||
'''NOTE''': This article uses Microsoft Exchange, a product that must be purchased and be installed separately. The Microsoft Exchange component that is included with Microsoft Windows 95 does not work with this article. | '''NOTE''': This article uses Microsoft Exchange, a product that must be purchased and be installed separately. The Microsoft Exchange component that is included with Microsoft Windows 95 does not work with this article. | ||
Line 120: | Line 120: | ||
On Error GoTo MAPITrap | On Error GoTo MAPITrap | ||
' Create the MAPI Session. | ' Create the MAPI Session. | ||
Set MapiSession = CreateObject( | Set MapiSession = CreateObject("Mapi.Session") | ||
' Log on to the session. If the ProfileName argument is omitted, | ' Log on to the session. If the ProfileName argument is omitted, | ||
' Microsoft Exchange prompts you for the profile to use. If the | ' Microsoft Exchange prompts you for the profile to use. If the | ||
' profile name is incorrect, you receive a runtime error. | ' profile name is incorrect, you receive a runtime error. | ||
MapiSession.Logon profilename:= | MapiSession.Logon profilename:="Steven Buchanan" | ||
' Add a message to the Outbox. | ' Add a message to the Outbox. | ||
Line 136: | Line 136: | ||
With MapiMessage | With MapiMessage | ||
Set MapiRecipient = MapiMessage.Recipients.Add | Set MapiRecipient = MapiMessage.Recipients.Add | ||
MapiRecipient.Name = | MapiRecipient.Name = "Nancy Davolio" | ||
MapiRecipient.Type = mapiTo | MapiRecipient.Type = mapiTo | ||
Set MapiRecipient = MapiMessage.Recipients.Add | Set MapiRecipient = MapiMessage.Recipients.Add | ||
MapiRecipient.Name = | MapiRecipient.Name = "Andrew Fuller" | ||
MapiRecipient.Type = mapiCc | MapiRecipient.Type = mapiCc | ||
Set MapiRecipient = MapiMessage.Recipients.Add | Set MapiRecipient = MapiMessage.Recipients.Add | ||
MapiRecipient.Name = | MapiRecipient.Name = "Michael Suyama" | ||
MapiRecipient.Type = mapiBcc | MapiRecipient.Type = mapiBcc | ||
Line 159: | Line 159: | ||
Set MapiAttachment = MapiMessage.Attachments.Add | Set MapiAttachment = MapiMessage.Attachments.Add | ||
With MapiAttachment | With MapiAttachment | ||
.Name = | .Name = "Customers.txt" | ||
.Type = mapiFileData | .Type = mapiFileData | ||
.Source = | .Source = "C:\Examples\Customers.txt" | ||
.ReadFromFile filename:= | .ReadFromFile filename:="C:\Examples\Customers.txt" | ||
.position = 2880 | .position = 2880 | ||
End With | End With | ||
' Assign the text, subject, and importance of the message. | ' Assign the text, subject, and importance of the message. | ||
.subject = | .subject = "My Subject" | ||
.Text = | .Text = "This is the text of my message." & vbCrLf & vbCrLf | ||
.importance = mapiHigh | .importance = mapiHigh | ||
Line 187: | Line 187: | ||
Resume MAPIExit | Resume MAPIExit | ||
Case Else | Case Else | ||
errMsg = MsgBox( | errMsg = MsgBox("Error " & errObj & " was returned.") | ||
Resume MAPIExit | Resume MAPIExit | ||
End Select | End Select |
Latest revision as of 12:28, 21 July 2020
Article ID: 153311
Article Last Modified on 1/19/2007
APPLIES TO
- Microsoft Access 97 Standard Edition
- Microsoft Access 95 Standard Edition
This article was previously published under Q153311
Advanced: Requires expert coding, interoperability, and multiuser skills.
SUMMARY
This article describes how you can use Automation to create and to send a Microsoft Exchange message.
This article assumes that you are familiar with Visual Basic for Applications and that you can create Microsoft Access applications by using the programming tools that are provided with Microsoft Access. For more information about Visual Basic for Applications, see your version of the "Building Applications with Microsoft Access" manual.
NOTE: This article uses Microsoft Exchange, a product that must be purchased and be installed separately. The Microsoft Exchange component that is included with Microsoft Windows 95 does not work with this article.
MORE INFORMATION
NOTE: The following code may not work properly if you have installed the Microsoft Outlook E-mail Security Update. For additional information about this update, depending on which version of Outlook you have, click the article numbers below to view the articles in the Microsoft Knowledge Base:
262631 OL2000: Information About the Outlook E-mail Security Update
262617 OL98: Information About the Outlook E-mail Security Update
You can send a MAPI mail message with the SendObject method of Microsoft Access. However, you cannot attach external files or set certain message properties, such as message importance, when you use the SendObject method.
There are five main steps in sending a MAPI message through Automation:
- Initialize and logon to the MAPI session.
- Create a new message and add the message to the Outbox.
- Add the recipients (To, Cc, and Bcc), and then resolve the names.
- Set valid properties, such as text, subject, and importance.
- Send the message.
To programmatically send a Microsoft Exchange message, follow these steps:
- Create a folder on drive C that is named Examples.
- Create a sample text file that is named Customers.txt in the Examples folder.
- Create a module, and then 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 to select OLE/Messaging 1.0 Object Library, and then click OK.
NOTE: If this object library is not available in the References list, click Browse, and then search your Windows\System folder for the file Mdisp32.tlb. Type the following procedure:
'-------------------------------------------------------------------- ' This procedure sets an object variable to the MAPI Session object ' using the CreateObject() function. Then, it logs on to the session ' using a predefined profile. As soon as you are logged on, ' the procedure creates ' a new message and adds it to the Messages collection of the Outbox ' of the user. Then, it creates two recipients (one on the TO: line and ' one on the CC: line) and then adds both to the Recipients collection ' of the message. Next, it resolves the names of all recipients. ' Then, it attaches a sample file before filling in the Subject, ' Text, and Importance attributes of the message. '-------------------------------------------------------------------- Sub SendMAPIMessage() Dim MapiSession As Object Dim MapiMessage As Object Dim MapiRecipient As Object Dim MapiAttachment As Object Dim Recpt Dim errObj As Long Dim errMsg On Error GoTo MAPITrap ' Create the MAPI Session. Set MapiSession = CreateObject("Mapi.Session") ' Log on to the session. If the ProfileName argument is omitted, ' Microsoft Exchange prompts you for the profile to use. If the ' profile name is incorrect, you receive a runtime error. MapiSession.Logon profilename:="Steven Buchanan" ' Add a message to the Outbox. Set MapiMessage = MapiSession.Outbox.Messages.Add ' Add the recipients of the message. Note, each recipient must be ' added separately to the Recipients collection of the Message ' object. With MapiMessage Set MapiRecipient = MapiMessage.Recipients.Add MapiRecipient.Name = "Nancy Davolio" MapiRecipient.Type = mapiTo Set MapiRecipient = MapiMessage.Recipients.Add MapiRecipient.Name = "Andrew Fuller" MapiRecipient.Type = mapiCc Set MapiRecipient = MapiMessage.Recipients.Add MapiRecipient.Name = "Michael Suyama" MapiRecipient.Type = mapiBcc ' Resolve each recipient's e-mail name. ' Starting with Outlook version 8.03 (ref. Q172623) ' OLE Messaging 1.0 was replaced with Active Messaging 1.1. ' Outlook 98 (version 8.5) replaced Active Messaging ' with Microsoft CDO (Collaborative Data Objects) 1.21. ' OLE Messaging 1.0 uses a zero-based Recipients collection; ' Active Messaging 1.1 and Microsoft CDO 1.21 are 1-based. For Recpt = 1 To .Recipients.Count .Recipients(Recpt).Resolve showdialog:=False Next ' Attach a file to the message. Set MapiAttachment = MapiMessage.Attachments.Add With MapiAttachment .Name = "Customers.txt" .Type = mapiFileData .Source = "C:\Examples\Customers.txt" .ReadFromFile filename:="C:\Examples\Customers.txt" .position = 2880 End With ' Assign the text, subject, and importance of the message. .subject = "My Subject" .Text = "This is the text of my message." & vbCrLf & vbCrLf .importance = mapiHigh ' View the message in Microsoft Exchange before sending. Set ' the ShowDialog argument to False if you want to send the ' message without viewing it in Microsoft Exchange. .Send showdialog:=True End With Set MapiSession = Nothing ' Clear the object variable. MAPIExit: Exit Sub MAPITrap: errObj = Err - vbObjectError ' Strip out the OLE automation error. Select Case errObj Case 275 ' User cancelled sending of message. Resume MAPIExit Case Else errMsg = MsgBox("Error " & errObj & " was returned.") Resume MAPIExit End Select End Sub
- Make sure to replace both the Mapisession.Login Profilename and the MapiRecipient.name with valid e-mail names.
- To test this procedure, type the following line in the Debug window, and then press ENTER:
SendMAPIMessage
Note that Microsoft Exchange is invoked with the sample message ready to send.
REFERENCES
For more information about Automation, search the Help Index for Automation or ask the Microsoft Access 97 Office Assistant.
For more information about referencing object libraries, search the Help Index for object libraries, or ask the Microsoft Access 97 Office Assistant.
Additional query words: mail inf
Keywords: kbhowto kbinterop kbprogramming KB153311