OpenObject Method
OpenObject Method
The OpenObject method binds to and opens data from the specified object.
Syntax
Sub OpenObject(ByVal Source as Object, ByVal InterfaceName as String)
HRESULT OpenObject(IUnknown* Source, BSTR InterfaceName);
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:
SameMsg.DataSource.OpenObject SameMsg, "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.
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
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;
}
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