HOWTO: Embedding a Document in a Message using Active Messaging

ID: Q169031

The information in this article applies to:

SUMMARY

Using Active Messaging, it is possible to embed a file in a message so that the contents of the file are viewed when the message is opened--for example, embedding a bitmap into the message so that the actual bitmap appears when you open the message.

MORE INFORMATION

The code provided is a sample of performing this task in Microsoft Visual Basic.

Steps to Embed a File in a Message

1. Create a MAPI Session object and call the Logon method:

      Dim oSession As Object
      Set oSession = CreateObject("MAPI.Session")
      oSession.Logon

2. Create a Message and Attachment object:

      Dim oMessage As Object
      Set oMessage = oSession.Outbox.Messages.Add

      Dim oAttachment As Object
      Set oAttachment = oMessage.Attachments.Add
      If oAttachment Is Nothing Then
         MsgBox "Unable to create new Attachment object"
         Stop
      End If
      'Specify where to place the attachment in the message
      oAttachment.Position = 1

3. Set the Type property of the Attachment to indicate an OLE object:

      ' Active Messaging 1.0
      oAttachment.Type = mapiOle

      ' Active Messaging 1.1
      oAttachment.Type = ActMsgOle

4. For Collaboration Data Objects (CDO) versions 1.0 and 1.1 only:

   Set the Source property of the Attachment to the class of the OLE
   object:

      oAttachment.Source = "Paint.Picture"

5. Use the ReadFromFile method of the Attachment to load the contents of
   the file for the OLE object and the Update method of the Message to save
   both the Attachment and the message:

      oAttachment.ReadFromFile "c:\test.bmp"
      oMessage.Update

6. Send the Message:

      'Goto the next line if an error occurs on the Send (usually
      'caused by the user cancelling the message)
      On Error Resume Next

      'Set ShowDialog to True because we have not set
      'the Recipient for the message
      oMessage.Send ShowDialog:=True

7. Call the Logoff method of the session:

      oSession.Logoff

Other Types of Message Attachments

Below are the four types of attachments that may be specified for a message and the corresponding Active Messaging 1.1 constants used when assigning the Type property of the attachment:

The return value or setting of the Source property depends on the value of the Type property, as described in the following table:

   Type property            Source property
   ----------------         ---------------------------------------------

   ActMsgFileData           Specifies the full path of the file that will
                            be attached when the ReadFromFile method is
                            called later. It is not necessary to set this
                            property if the source is specified in the call
                            to the Add method of the Attachments
                            collection.

   ActMsgFileLink           Specifies a full path name in a universal
                            naming convention (UNC) format, such as
                            \\Sales\Info\Product\News.doc.

   ActMsgOLE                Specifies the registered OLE class name of the
                            attachment, such as Word.Document or
                            PowerPoint.Show. The actual file contents would
                            be loaded with the ReadFromFile method.

   ActMsgEmbeddedMessage    Specifies the unique identifier of the message
                            to be embedded; returns the embedded Message
                            object.

REFERENCES

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

Microsoft Developer Network, Platform SDK, "Source Property (Attachment Object)"
Keywords          : kbcode kbActMsg kbCDO100 kbCDO110 kbCDO120 kbCDO121 kbVBp400 kbVBp500 kbGrpMsg OleMsg 
Version           : WINDOWS;1.0,1.1,4.0,5.0,1.21
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 9, 1999