Platform SDK: CDO for Windows 2000

Saving Messages into ADO Stream Objects

To save a serialized message from a Message object into a Microsoft® ActiveX® Data Objects (ADO) Stream object, perform the following steps:

  1. Retrieve the IDataSource interface on the Message object.
  2. Create or otherwise obtain a _Stream object reference on an ADO Stream object.
  3. Using the IDataSource interface on the Message object, call the OpenObject method, passing the _Stream object reference as the first argument, and the string "_Stream" as the second.
[Visual Basic]
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
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]
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
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 SaveMessageToFile(iMsg, Filepath)
    Dim Stm
    Set Stm = CreateObject("ADODB.Stream")
    Stm.Type = adTypeText  ' 2
    Stm.Charset = "US-ASCII"
    Stm.Open

    Dim iDsrc
    Set iDsrc = iMsg.DataSource
    iDsrc.SaveToObject Stm, "_Stream"
    Stm.SaveToFile Filepath, adSaveCreateOverWrite
    
End Sub

See Also

IDataSource Interface

IDataSource.SaveToObject Method

IDataSource.OpenObject Method