How to Send Attachment & Show Image in VB 3.0 Using Msmapi.vbx

Last reviewed: May 2, 1996
Article ID: Q138534
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic Programming System for Windows, version 3.0

SUMMARY

This article shows by example how to send a message with an attachment, where the attachment is the actual document (not an icon) and the object is visible in the body of the message. This can be accomplished using the MAPI controls supplied in Visual Basic 3.0.

MORE INFORMATION

Sending an attachment with a message using the MAPI custom controls is easy. You should first establish your MAPI Connection, and then go into Compose mode. For more information about this process, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q113033
   TITLE     : How to Send a Mail Message Using Visual Basic MAPI Controls

You would then start setting properties for the MAPI Messages control. The code for attaching a file follows:

   Form1.MapiMessages1.AttachmentPathName = "C:\WINNT35\ARCADE.BMP"
   Form1.MapiMessages1.AttachmentName = "ARCADE.BMP"
   Form1.MapiMessages1.AttachmentType = 2  ' ATTACHTYPE_SOLE an SOLE is a
                                           ' static OLE object attachment

This code sends the attachment with the rest of the message, but the attachment shows up within the message body as an icon. In order to view the actual data, you must double-click the icon and start the necessary application, which may not be what you need.

Step-by-Step Example to Send True Image of Attachment Within Message Body

The following example shows how to send the attachment within the message body and have it display as its true image. (Please note that the code below was created using MSMail as the mail client.)

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Place the following controls on Form1:

       Control                 Name
       ------------------------------------
       OLE Control             OLE1
       MAPI Session Control    MapiSession1
       MAPI Messages Control   MapiMessages1
       One Command Button      saveOLE
       One Command Button      sendMessage
    
    

  3. Add the following code to the general declarations section of Form1:

    Dim vstAttachedOLEFile As String

       'MAPI constants from CONSTANT.TXT file:
       Const ATTACHTYPE_DATA = 0
       Const ATTACHTYPE_EOLE = 1
       Const MESSAGE_COMPOSE = 6
       Const MESSAGE_RESOLVENAME = 13
       Const MESSAGE_SEND = 3
       Const OLE_SAVE_TO_OLE1FILE = 18
       Const RECIPTYPE_TO = 1
       Const SESSION_SIGNON = 1
       Const SESSION_SIGNOFF = 2
    
    

  4. Add the following code to the appropriate event procedures:

       Sub saveOLE_Click ()
    
          Dim iFileNum As Integer
    
          ' Get a free file handle
          iFileNum = FreeFile
    
          ' So it won't raise an error if the file doesn't exist, disable error
          ' checking.
          On Error Resume Next
          vstAttachedOLEFile = "c:\temp\test.bmp"
          Kill vstAttachedOLEFile
          On Error GoTo 0
    
          ' Set the SourceDoc property to the file you want to attach to the
          ' message.
          OLE1.SourceDoc = "C:\winnt35\arcade.bmp"
    
          ' Embed the file in the OLE control
          OLE1.Action = 0
          DoEvents
    
          ' Open the file that will contain the OLE 1.0 compliant object
          ' information.
          Open vstAttachedOLEFile For Binary As #iFileNum
          OLE1.FileNumber = iFileNum
    
          ' Save the object as an OLE 1.0 compliant object.
          OLE1.Action = OLE_SAVE_TO_OLE1FILE
    
          Close iFileNum
    
       End Sub
    
       Sub sendMessage_Click ()
    
          ' Open up a MAPI session:
          MapiSession1.Action = SESSION_SIGNON
    
          ' Point the MAPI messages control to the open MAPI session:
          MapiMessages1.SessionID = form1.MapiSession1.SessionID
    
          ' Start a new message
          MapiMessages1.Action = MESSAGE_COMPOSE
    
          ' Set the subject of the message:
          MapiMessages1.MsgSubject = "This is the subject."
    
          ' Set the message content:
          MapiMessages1.MsgNoteText = "       This is the mail message."
    
          form1.MapiMessages1.AttachmentIndex = _
             form1.MapiMessages1.AttachmentCount
          form1.MapiMessages1.AttachmentPathName = vstAttachedOLEFile
          form1.MapiMessages1.AttachmentName = vstAttachedOLEFile
          form1.MapiMessages1.AttachmentType = ATTACHTYPE_EOLE
          form1.MapiMessages1.AttachmentPosition = 1
    
          ' Set the recipient type. RECIPTYPE_TO sends message to recipient:
          MapiMessages1.RecipType = RECIPTYPE_TO
    
          ' Set the recipient's E-Mail name.
          ' You can have multiple recipients separated by semicolons
          ' Change to a valid e-mail name
          MapiMessages1.RecipDisplayName = "joecool"
    
          ' MESSAGE_RESOLVENAME checks to ensure the recipient is valid and
          ' puts the recipient address in MapiMessages1.RecipAddress
          ' If the E-Mail name is not valid, a trappable error will occur.
          MapiMessages1.Action = MESSAGE_RESOLVENAME
    
          ' Send the message:
          MapiMessages1.Action = MESSAGE_SEND
    
          ' Close MAPI mail session:
          MapiSession1.Action = SESSION_SIGNOFF
    
       End Sub
    
    
NOTE: When Microsoft Mail was originally developed, the OLE 1.0 standard was still predominant. This is why the object must be saved into this version of an OLE object.


Additional reference words: 3.00 send
KBCategory: kbinterop kbole kbcode kbhowto
KBSubcategory: APrgOther


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: May 2, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.