OpenObject Method

 

Description

 

The OpenObject method binds to and opens data from the specified object.

 

C++ Syntax

 

HRESULT OpenObject(

IUnknown* Source,

BSTR InterfaceName

);

 

Visual Basic Syntax

 

Sub OpenObject(

ByVal Source as Object,

ByVal InterfaceName as String

)

 

PowerBASIC Syntax

 

METHOD OpenObject( _

BYVAL Source AS IUnknown, _

BYVAL InterfaceName AS STRING _

)

 

Parameters

 

Source

 

The object reference (interface) to the object to open.

 

InterfaceName

 

A string that indicates the type of interface passed in the first argument. Can be one of the following strings: IBodyPart, _Stream, IStream, and IMessage.

 

Remarks

 

You can use the IDataSource.OpenObject method to open messaging data from other objects that implement an appropriate interface; for example, you may have a message in serialized format that is contained within a Microsoft® ActiveX® Data Objects (ADO) Stream object. You can open the object and load the serialized message data into a Message object. Similarly, you can have the serial message data in an object that exposes the IStream, IBodyPart, and IMessage interfaces.

 

Note  Circular binding is not supported. You cannot use OpenObject to open the same object, such as the following:

 

[Visual Basic]

SameMsg.DataSource.OpenObject SameMsg, "IMessage"

 

[PowerBASIC]

SameMsg.DataSource.OpenObject SameMsg, UCODE$("IMessage")

 

Also, you cannot bind to BodyPart objects within the Message objects body part hierarchy. You must use a separate Message object.

 

One example how you can use the OpenObject method is the manipulation of encapsulated messages. A received message can contain a body part for which the ContentMediaType property is message. You might want to see the address of the sender and the subject of this encapsulated message but the body part object does not expose properties for these. You can create a new Message object and call the IDataSource.OpenObject method, passing the object reference of the encapsulated message body part. This effectively imports the contents of the encapsulated message into the Message object. You can then examine and manipulate the properties and contents of the message by using the new Message object. To save any changes you have made, you can call the IDataSource.Save method, and the modified message contents will be saved back to the source body part.

 

If you are using C++, do not pass the IStream interface to the OpenObject method if you want the interface that is returned by IDataSource.Source to be accessible to scripting languages.

 

If the interface name passed (the InterfaceName) is not recognized, the method raises an exception.

 

After a successful call to OpenObject, 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.

 

Visual Basic Example

 

' Reference to Microsoft ActiveX Data Objects 2.5 Library

' Reference to Microsoft CDO for Windows 2000 Library

Function LoadMessageFromFile(Path As String) As Message

  Dim Stm As New Stream

  Stm.Open

  Stm.LoadFromFile Path

  Dim iMsg As New CDO.Message

  Dim iDsrc As IDataSource

  Set iDsrc = iMsg

  iDsrc.OpenObject Stm, "_Stream"

  Set LoadMessageFromFile = iMsg

End Function

 

Function ExtractMessage(iBp As IBodyPart) As Message

  Dim iMsg As New CDO.Message

  Dim iDsrc As IDataSource

  Set iDsrc = iMsg

  iDsrc.OpenObject iBp, "IBodyPart"

  Set ExtractMessage = iMsg

End Function

 

PowerBASIC Example

 

#INCLUDE "cdoex.inc"

 

FUNCTION LoadMessageFromFile (strPath AS STRING) AS CDO_IMessage

 DIM Stm AS ADOStream

 Stm = NEWCOM "ADODB.Stream"

 Stm.Open

 Stm.LoadFromFile UCODE$(strPath)

 DIM iMsg AS CDO_IMessage

 iMsg = NEWCOM "CDO.Message"

 DIM iDsrc AS CDO_IDataSource

 iDsrc = iMsg

 iDsrc.OpenObject Stm, UCODE$("_Stream")

 FUNCTION = iMsg

END FUNCTION

 

FUNCTION ExtractMessage (iBp AS CDO_IBodyPart) AS CDO_IMessage

 DIM iMsg AS CDO_IMessage

 DIM iDsrc AS CDO_IDataSource

 iMsg = NEWCOM "CDO.Message"

 iDsrc = iMsg

 iDsrc.OpenObject iBp, UCODE$("IBodyPart")

 ExtractMessage = iMsg

END FUNCTION

 

C++ Example

 

IMessagePtr ExtractMessage(IBodyPartPtr iBp)

{

    IMessagePtr iMsg(__uuidof(Message));

    IDataSourcePtr iDsrc;

    iDsrc = iMsg;

 

    try

    {

          iDsrc->OpenObject(iBp,_bstr_t("IBodyPart"));

    }

    catch(_com_error error)

    {

          throw error;

    }

    return iMsg;

 

}

 

IMessagePtr Load_Message_from_File(_bstr_t path)

{

    /*

    ** This example shows a common use of the ADO Stream

    ** object with CDO, namely, opening a serialized

    ** message from a file on disk and loading it into

    ** a CDO Message object.

    **/

 

    _StreamPtr  pStm(__uuidof(Stream));

    _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);

    try {

          pStm->raw_Open(

                          varOptional,

                          adModeUnknown,

                          adOpenStreamUnspecified,

                          NULL,

                          NULL);

          pStm->LoadFromFile(path);

    }

    catch(_com_error e)

    {

          throw e;

    }

 

    IMessagePtr iMsg(__uuidof(Message));

    IDataSourcePtr iDsrc;

    iDsrc = iMsg;

 

    try {

          iDsrc->OpenObject(pStm,_bstr_t("_Stream"));

    }

    catch(_com_error e)

    {

          throw e;

    }

    return iMsg;

}

 

VBScript Example

 

Sub EmbedMessage(iMsg As CDO.Message, iBp As IBodyPart)

  Dim iDsrc

  Set iDsrc = iMsg.DataSource

  iDsrc.SaveToObject iBp, "IBodyPart"

End Sub

 

Function ExtractMessage(iBp As IBodyPart) As Message

  Dim iMsg

  Set iMsg = CreateObject("CDO.Message")

  Dim iDsrc

  Set iDsrc = iMsg.DataSource

  iDsrc.OpenObject iBp, "IBodyPart"

  Set ExtractMessage = iMsg

End Function

 

Function LoadMessageFromFile(Path) As Message

  Dim Stm

  Set Stm = CreateObject("ADODB.Stream")

  Stm.Open

  Stm.LoadFromFile Path

  Dim iMsg

  Set iMsg = CreateObject("CDO.Message")

  Dim iDsrc

  Set iDsrc = iMsg

  iDsrc.OpenObject Stm, "_Stream"

  Set LoadMessageFromFile = iMsg

End Function

 

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

 

Valid XHTML 1.0 Transitional