Working With Attachments

Once you've retrieved the messages containing the information you need, you'll need to process the information contained in the message. This may mean simply displaying it for a user to review, or it can also mean unbundling attachments that are included with the message.

When you read the message, you can use the FileCount NumFiles variable, returned with the MAPIReadMail call, to determine how many attachments are included. When you issue the MAPIReadMail call, MAPI opens not only the mail messages, but it also extracts the attachment, placing it in the user's TEMP subdirectory. All you need to do to retrieve the attachment and begin working with it, then, is to review the MAPIFile structure for the file name. Use this name to locate the file and begin working with it.

Here's some sample code that will help illustrate this capability:


If NumFiles > 0 Then
    'since there is a file attachment, import it.
    MsgBox "Importing " & tFiles(0).PathName & "..."

    'Import the files
    DoCmd TransferSpreadsheet A_IMPORT, 5, "Company", tFiles(0).PathName, True
    MsgBox tFiles(0).PathName & " imported.
End If

In this example, we first check the number of available files to make sure an attachment was present. If so, we indicate to the user that we'll be importing a file and then use the Microsoft Access Basic DoCmd function to run the TransferSpreadsheet method and import the sheet to the Company table.

It's interesting to note that MAPI has updated the path to the file, showing the path to the user's TEMP subdirectory in PathName. When you add an attachment, you use this field to specify where the file is found, then, when the message is read, MAPI updates the field to show the location of the attachment when it's unbundled.