This documentation is archived and is not being maintained.

SaveToContainer Method

Exchange Server 2003

SaveToContainer Method

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Binds to and saves data into a new item in the folder/container specified by URL. The item name is a generated globally unique identifier (GUID).

Applies To

Type Library

Microsoft CDO for Exchange 2000 Library

DLL Implemented In



        [Visual Basic]Function SaveToContainer
    ContainerURL As String,     [ActiveConnection As Object],     [Mode As ConnectModeEnum],     [CreateOptions As RecordCreateOptionsEnum],     [Options As RecordOpenOptionsEnum],     [UserName As String],     [Password As String]

        [C++]HRESULT SaveToContainer
    BSTR ContainerURL,
    ConnectModeEnum Mode,
    RecordCreateOptionsEnum CreateOptions,
    RecordOpenOptionsEnum Options,
    BSTR UserName,
    BSTR Password


The URL of the folder/collection resource which the item is to be saved.
Specifies the optional connection to use when saving. This is an IDispatch interface to a Microsoft ActiveX Data Objects (ADO) Connection object. A new Connection (session) object is implicitly created if none is specified.
Access mode. This value is always ORed with adModeReadWrite. That is, at least read-write access is required to save the item to the container/folder.
Set of flags to specify creation options. The values adFailIfNotExists and adOpenIfExists should not be used. If used, an E_INVALIDARG exception is raised.
This specifies options flag(s) used when opening the source. Your setting is always ORed with adOpenSource.
Not Supported - Reserved for future use. Used to pass a user name for authentication.
Not Supported - Reserved for future use. Used to pass a password for authentication.

Return Value

Returns S_OK if successful, or an error value otherwise.


The specified URL must identify a valid URL namespace that the OLE DB 2.5 root binder can resolve to a registered provider binder. Once resolved, the URL must conform to that provider binder's URL semantics.

The enumerated values and their semantic definitions are defined as part of the ADO type library and documentation. Consult the ADO 2.5 documentation for a list of valid enumeration values and their intended purposes. Use of various enumerated values and their intended meanings are specific to a particular OLE DB 2.5 provider.

Restrictions on what types of data can be saved into a particular resource and how that data is handled by an implementation of the IDataSource interface is not part of this definition. Consult the appropriate Component Object Model (COM) class reference for further information.

The SaveToContainer method differs from SaveTo in that the name for the resource is automatically generated for you in the container collection specified by URL. The name generated by this method is not defined here and is implementation specific.

After successful completion of this method, you can retrieve the full URL to the new object to which you are currently bound by examining the IDataSource.SourceURL property.


        [Visual Basic]

' Reference to Microsoft CDO for Exchange 2000 Library
' Reference to Microsoft ActiveX Data Objects 2.5 Library

Sub SaveEmbeddedPartsToFolder(iMsg As CDO.message, Url As String)

    Dim iMsgB As New CDO.message
    Dim iDsrcB As CDO.IDataSource
    Dim iFldr As New Folder
    Dim iDsrcFldr As CDO.IDataSource
    Dim Conn As New ADODB.Connection
    Dim iBp As IBodyPart
    Dim iBps As IBodyParts
    Dim ContType As String

    Conn.Provider = "ExOLEDB.DataSource"
    Conn.Open Url

    Set iDsrcFldr = iFldr
    iDsrcFldr.Open Url, Conn

    Set iDsrcB = iMsgB

    Set iBp = iMsg
    Set iBps = iBp.BodyParts

    For Each iBp In iBps
        ContType = iBp.ContentMediaType
        If ContType = "message/rfc822" Then

            iDsrcB.OpenObject iBp, "IBodyPart"
            iDsrcB.SaveToContainer Url, Conn, adModeReadWrite, adCreateOverwrite
            Debug.Print "Saved message has URL " & iDsrcB.SourceURL

        ElseIf ContType = "application/octet-stream" Or _
                    ContType = "application/x-msdownload" Then

            Dim iItem As New Item
            Dim Flds As ADODB.Fields
            Dim Fld As ADODB.Field
            Dim Stm1 As Stream
            Dim Stm2 As Stream

            Set Stm1 = iItem.GetStream

            ' Get Decoded BodyPart stream
            Set Stm2 = iBp.GetDecodedContentStream
            ' Copy bodypart stream to resource stream
            Stm2.CopyTo Stm1

            ' Commit
            iItem.DataSource.SaveToContainer Url, Conn, adModeReadWrite

        End If
    Next iBp
End Sub
' Reference to Microsoft CDO for Exchange 2000 Library
' Reference to Microsoft ActiveX Data Objects 2.5 Library

Function CreateAndSaveMessageToContainer(Url1 As String) As CDO.message

    Dim iDsrc As CDO.IDataSource
    Dim Conn As New ADODB.Connection
    Conn.Provider = "ExOLEDB.DataSource"
    Conn.Open Url1

    Dim iMsg As New CDO.message

    With iMsg
        .To = ""
        .From = ""
        .Subject = "Here is the subject"
        .TextBody = "Here is the text of the message"
    End With

    Set iDsrc = iMsg

    iDsrc.SaveToContainer Url1, _
                Conn, _

    Set CreateAndSaveMessageToContainer = iMsg

End Function


 You must have the following paths in your
 INCLUDE path.
 %CommonProgramFiles%\microsoft shared\cdo

#import <msado15.dll> no_namespace
#import <cdoex.dll> no_namespace
#include <iostream.h>

void SaveMessageToFolder2(IMessagePtr pMsg, bstr_t url) {

   _ConnectionPtr pConn(__uuidof(Connection));

   if(pMsg == NULL)
      throw _com_error(E_INVALIDARG);
   // for passing optional variants, we may need this
   _variant_t varOpt(DISP_E_PARAMNOTFOUND,VT_ERROR);
   IDataSourcePtr pDsrc = pMsg;

   pConn->Provider = "ExOLEDB.DataSource";
   try {
   catch(_com_error e) {
      throw e;

   try {
            variant_t((IDispatch*)pConn,true), //Connection from Folder bind above
            (RecordOpenOptionsEnum) NULL,
   catch(_com_error e) {
      cerr << "Error saving message to folder!" << endl;
      throw e;

<job id="idatasource_savetocontainer">
<reference object="adodb.record"/>
<reference object="cdo.message"/>
<script language="vbscript">

    Dim iDsrc
    Dim iMsg
    Dim Conn
    Dim InfoNT
    Dim Info
    Dim sFolderUrl

    Set Info   = CreateObject("ADSystemInfo")
    Set InfoNT = CreateObject("WinNTSystemInfo")
    Set Conn   = CreateObject("ADODB.Connection")
    Set iMsg   = CreateObject("CDO.Message")

    sFolderUrl = "http://" & InfoNt.ComputerName & "." & Info.DomainDNSName &  _

    Conn.Provider = "ExOLEDB.DataSource"
    Conn.Open sFolderUrl

    ' ...

    With iMsg
        .To   = ""
        .From = ""
        .Subject = "Here is the subject"
        .TextBody = "Here is the text of the message"
    End With

    Set iDsrc = iMsg.DataSource
    iDsrc.SaveToContainer sFolderUrl, Conn
    Wscript.Echo "Saved To and bound to item at URL: " & iDsrc.SourceURL

    iMsg.TextBody = iMsg.TextBody & vbCrLf & "And this is another line."