Platform SDK: CDO for Windows 2000

SaveToObject Method

The SaveToObject method binds to and saves data into the specified object.

[Visual Basic,VBScript]
Sub SaveToObject(Source as Object, InterfaceName as String)
[C++]
HRESULT SaveToObject(IUnknown* Source , BSTR InterfaceName);
[IDL]
HRESULT SaveToObject(IUnknown* Source , BSTR InterfaceName);

Parameters

Source
The interface on the object in which to save the data.
InterfaceName
A string that indicates the type of interface passed in the first argument.

Remarks

Calling the SaveToObject method rebinds the object with the target object as the new data source. The SaveToObject method makes a copy of the data in the current object to the target object, and all subsequent calls to IDataSource.Save saves the data into the newly bound object. Each object only ever has one data source into which the Save method copied data.

After a successful call to SaveToObject, the IDataSource.SourceClass property returns the interface name specified by the InterfaceName parameter. Likewise, the IDataSource.Source property returns the object reference specified by the Source property.

Example

[Visual Basic]
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Sub EmbedMessage(iMsg As CDO.Message, iBp As IBodyPart)
    Dim iDsrc As IDataSource
    Set iDsrc = iMsg
    iDsrc.SaveToObject iBp, "IBodyPart"
End Sub

Sub SaveMessageToFile(iMsg As CDO.Message, Filepath As String)
    Dim Stm As New Stream
    Stm.Open
    Stm.Type = adTypeText
    Stm.Charset = "US-ASCII"
    
    Dim iDsrc As IDataSource
    Set iDsrc = iMsg
    iDsrc.SaveToObject Stm, "_Stream"
    
    Stm.SaveToFile Filepath, adSaveCreateOverWrite
    
End Sub
[C++,IDL]
void EmbedMessage( IMessagePtr iMsg, IBodyPartPtr iBp)
{
      IDataSourcePtr iDsrc;
      iDsrc = iMsg;

      try 
      {      
            iDsrc->SaveToObject(iBp,_bstr_t("IBodyPart"));
      }
      catch(_com_error error)
      {
            throw error;
      }

}

void Save_Message_to_File(IMessagePtr iMsg, bstr_t filename)
{
      /*
      ** This example shows a common use of the ADO Stream
      ** object with CDO, namely, saving a serialized
      ** message to disk using an ADO Stream object.
      **/
      _StreamPtr  pStm(__uuidof(Stream));
      IDataSourcePtr iDsrc;
      iDsrc = iMsg;

      _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
      pStm->raw_Open( 
                     varOptional,  
                     adModeUnknown, 
                     adOpenStreamUnspecified,
                     NULL,
                     NULL);
      pStm->Type = adTypeText;
      pStm->Charset = "US-ASCII"; // apparently, we need this!
      try 
      {      
            iDsrc->SaveToObject(pStm,_bstr_t("_Stream"));
      }
      catch(_com_error error)
      {
            throw error;
      }

      try {
            pStm->SaveToFile(filename,adSaveCreateOverWrite);
      }
      catch(_com_error e)
      {
            throw e;
      }
}

[VBScript]
Sub EmbedMessage(iMsg As CDO.Message, iBp As IBodyPart)
    Dim iDsrc
    Set iDsrc = iMsg.DataSource
    iDsrc.SaveToObject iBp, "IBodyPart"
End Sub

Sub SaveMessageToFile(iMsg, Filepath)
    Dim Stm
    Set Stm = CreateObject("ADODB.Stream")
    Stm.Open
    Stm.Type = adTypeText ' 2
    Stm.Charset = "US-ASCII"
    
    Dim iDsrc
    Set iDsrc = iMsg.DataSource
    iDsrc.SaveToObject Stm, "_Stream"
    
    Stm.SaveToFile Filepath, adSaveCreateOverWrite
    
End Sub