Share via


Navigating CDO Interfaces with VBScript

Topic Last Modified: 2009-07-27

Unlike the Microsoft® ActiveX® Data Objects (ADO) and Active Directory® Service Interfaces (ADSI) object models, the Collaboration Data Objects (CDO) component provides a consistent interface-driven object model for all programming languages.

For example, most ADSI objects expose multiple interfaces to clients that can bind directly to Component Object Model (COM) interfaces. However, for Automation clients such as scripting languages, each object exposes a single implementation of the IDispatch interface that is adjusted to include all methods and properties on all Automation interfaces on the object.

With CDO, however, the notion of the interface is preserved, and therefore you must navigate to the interface that exposes the functionality you require. Microsoft Visual Basic® Scripting Edition (VBScript) provides no inherent mechanism for interface navigation, so CDO COM interfaces have been designed to provide this mechanism. Many CDO interfaces provide an implementation of the GetInterface method. This method takes a string identifying the desired interface and returns that interface, if it is exposed. The method acts as an IUnknown::QueryInterface on the object for use by languages that do not support interface navigation. One very common example is the CDO IMessage interface. Objects that expose the IMessage interface normally also expose the IDataSource and IBodyPart interfaces as well. You can use IMessage::GetInterface to navigate to these interfaces by using scripting languages (or anytime you can access the object only through the IDispatch interface).

Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iDsrc
Set iDsrc = iMsg.GetInterface("IDataSource")
Dim iBp
Set iBp = iMsg.GetInterface("IBodyPart")

...

Many CDO interfaces also expose properties that return interfaces on the same object. Using the IMessage interface example again, the IDataSource and IBodyPart interfaces are accessible using the IMessage::DataSource and IMessage::BodyPart properties, as shown in the following code:

Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iDsrc
Set iDsrc = iMsg.DataSource
Dim iBp
Set iBp = iMsg.BodyPart
...

In this case, you can use either the corresponding property on the interface or the GetInterface method to navigate these interfaces.

You cannot access interfaces on objects that are not Automation-compatible. For example, to allow C++ programmers access to raw OLE DB interfaces for items, instances of the CDO Message COM class expose the OLE DB IRow interface. This interface cannot be used by scripting languages and therefore is never returned by a property on an interface or through GetInterface. It is, of course, accessible through QueryInterface.