Using SendObject to Send A Mail Message

As you've seen, using MAPI to send messages is quite straight-forward. You'll probably find that you'll come across instances where you require more programmatic control over the information that is sent.

In these cases, you may be able to use the Microsoft Access Basic SendObject function. With SendObject, you specify all of this information from within Microsoft Access Basic. If you do leave out a piece of information that is required, MAPI will step in and ask the user to clarify that item.

You may want to add the SendObject capabilities to a menu that is pertinent to your application. You can place a reference from the menu to an Microsoft Access Basic module routine that packages up the information and sends it to the defined recipient.

As an example, the Function below, ABSendObject function, send a copy of a table, hard-coded as "Company," to the destination you set up. You'll need to provide a valid destination address in sRecipient. The name should be unique in your mail system to avoid MAPI needing to resolve the name when the message is sent.

If you do not specify a unique name, the Send Note dialog will be shown and you will have to specify the name to send the message to.


Function ABSendObject ()
    'send an object, using MAPI, from Access to
    'another system - this routine is hardcoded
    'to send the company table.

    Dim sRecipient As String
    sRecipient = "Steve Wynkoop;"

    'This example specifies the user to send the object to.
    MsgBox "Sending message to: " & sRecipient & "..."
    DoCmd SendObject A_TABLE, "Company", A_FORMATXLS, sRecipient, , , "Copy of the company database.", , False
    
    MsgBox "Message sent."
End Function

The table below shows the parameters expected by the SendObject method. As indicated before, if you omit these, MAPI will attempt to resolve the information, either by asking the user to clarify something, or by not sending the message and providing your calling routine with an error message.

ObjectType

Use the Microsoft Access intrinsic constants of A_TABLE, A_QUERY, A_FORM, A_REPORT or A_MODULE.

ObjectName

This is the name of what you're going to send. This can be a form or query name, etc.

OutputFormat

These file types are represented by the A_FORMATXLS, A_FORMATRTF and A_FORMATTXT intrinsic constants.

To, CC, BCC, Subject and MessageText

All items are strings, providing the addressee's name, the message subject and any associated message text. BCC is not currently supported by the MSMail client.

EditMessage

True or False. This controls the display of the send note dialog box.


If the recipient information provided is not valid, the message will not be sent. Instead, your routine will receive an error message indicating that the recipient was not valid.

Error message received when a recipient is not valid