ACC: Using Automation to Send a Microsoft Exchange Message

Last reviewed: August 28, 1997
Article ID: Q153311
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article demonstrates how to create and send a Microsoft Exchange message using Automation.

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 your version of the "Building Applications with Microsoft Access" manual.

NOTE: This article uses Microsoft Exchange, a product which must be purchased and installed separately. The Microsoft Exchange component which ships with Windows 95 will not work with this article.

MORE INFORMATION

Microsoft Access provides the SendObject method which enables you to send a MAPI mail message. However, the SendObject method provides no way to attach external files or set certain message properties, such as message importance.

There are five main steps in sending a MAPI message through Automation:

  • Initializing and logging onto the MAPI session
  • Creating a new message and adding it to the Outbox
  • Adding the recipients (To, CC, and BCC) and resolve names
  • Setting valid properties, such as text, subject, and importance
  • Sending the message

To programmatically send a Microsoft Exchange message, follow these steps:

  1. Create a folder on drive C named Examples. Create a sample text file named Customers.txt in the Examples folder.

  2. Create a module and type the following line in the Declarations section if it is not already there:

          Option Explicit
    

  3. On the Tools menu, click References.

  4. In the References box, select OLE/Messaging 1.0 Object Library, and click OK.

    NOTE: If this object library is not available in the References list, click the Browse button and search your Windows\System folder for the file Mdisp32.tlb.

  5. 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. Once logged on, the procedure creates
        ' a new message and adds it to the Messages collection of the user's
        ' OutBox. Then, it creates two recipients (one on the TO: line and
        ' one on the CC: line) and 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 will 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.
                For Recpt = 0 To .Recipients.Count - 1
                    .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
    
    

  6. Be sure to replace both the Mapisession.Login Profilename and the MapiRecipient.name with valid e-mail names.

  7. 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
Keywords : AutoGnrl kbinterop IntpOleA
Technology : kbole
Version : 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.