Sdílet prostřednictvím


Saving Messages into ADO Stream Objects

Topic Last Modified: 2006-06-11

To save a serialized message from a Collaboration Data Objects (CDO) Message object to an ADO Stream object

  1. Retrieve the IDataSource interface on the Message object.
  2. Create or otherwise obtain a _Stream object reference on a Microsoft® ActiveX® Data Objects (ADO) Stream object.
  3. Use the IDataSource interface on the Message object to call OpenObject. Pass the _Stream object reference as the first argument, and the string "_Stream" as the second.

Example

Visual Basic

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

' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
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 "c:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
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

' Note: It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
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