HOWTO: Embedding a Document in a Message using Active Messaging

Last reviewed: November 21, 1997
Article ID: Q169031
The information in this article applies to:
  • Collaboration Data Objects (CDO), versions 1.0, 1.1
  • Microsoft Visual Basic Enterprise Edition for Windows, versions 4.0, 5.0

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. 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 contents of a file (you see an icon in the message representing the data in the file), ActMsgFileData
  • A link to a file (usually on a file server), ActMsgFileLink
  • An OLE object (example above), ActMsgOLE
  • An embedded message, ActMsgEmbeddedMessage

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           Not used. The source for this type of
                            attachment must be specified in the call to the
                            Add method or later with the ReadFromFile
                            method.

   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 ActMsg OLEMSG
Technology        : kbole
Version           : 1.0 1.1 4.0 5.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


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: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.