Extracting Attachments

To determine if a message contains one or more file attachments, the simplest method is to check the value of the Attachment property for each message part. This property returns the name of the file attachment in the current message part or returns an empty string if there is no file attachment. The control checks for standard MIME attachments as well as non-standard uuencoded files which are embedded in the body of the message. The following code demonstrates how this could be done:

bHasAttachments = False

For nPart = 0 To InternetMail1.MessageParts - 1
    InternetMail1.MessagePart = nPart
    If Len(InternetMail1.Attachment) > 0 Then
        bHasAttachments = True
        Exit For
    End If
Next

To extract the attached file, use the ExtractFile method and specify the name of the attachment on the local system. For example:

For nPart = 0 To InternetMail1.MessageParts - 1
    InternetMail1.MessagePart = nPart
    If Len(InternetMail1.Attachment) > 0 Then
        strFileName = "c:\temp\" & InternetMail1.Attachment
        nError = InternetMail1.ExtractFile(strFileName)
        If nError Then
            MsgBox "Unable to extract " & InternetMail1.Attachment, _
                   vbExclamation
        End If
    End If
Next

If the file is extracted from the message successfully, the ExtractFile method will return a value of zero, otherwise an error code will be returned.


Copyright © 2008 Catalyst Development Corporation. All rights reserved.