Microsoft Excel Specifics

This section describes some differences between a generic implementation of an OLE Automation Controller and a controller specifically designed to work with Microsoft Excel.

IDispatch and IUnknown

Because Microsoft Excel supports the IDispatch interface, your application does not need to get the IUnknown interface and then query Microsoft Excel for the IDispatch interface. You can use the CoCreateInstance function to directly access the IDispatch interface because you know Microsoft Excel supports IDispatch.

Passed-in Objects

When Visual Basic in Microsoft Excel passes an object reference to your DLL function, the DLL can go directly to step 4 in the process shown in the section on Idispatch Step by Step and access the IDispatch interface directly from the object reference. The PropertyPut and PropertyGet examples in the following section show how this is done in a DLL.

Variant Data Types

Visual Basic in Microsoft Excel does not support all the data types supported by the VARIANT structure. The following table shows the allowed data types and their value constants. Microsoft Excel never returns a variant with a data type not shown on this list.

Data Type

Variant Constant

Boolean

VT_BOOL

Currency (scaled integer)

VT_CY

Date

VT_DATE

Double (double-precision floating-point)

VT_R8

Integer

VT_I2

Long (long integer)

VT_I4

Object

VT_DISPATCH

Single (single-precision floating-point)

VT_R4

String

VT_BSTR


Microsoft Excel coerces other data types into one of the supported data types.

Exception Handling

When an error occurs during the execution of the IDispatch Invoke function, Microsoft Excel returns an error code or returns the DISP_E_EXCEPTION error and places information about the error in an EXCEPINFO structure. The error code in the exception info structure will be one of the trappable error values listed in the Visual Basic Help topic "Trappable Errors." When the exception info structure is filled in, your application is responsible for freeing the strings in the structure. A memory leak will result if you do not free the strings before reusing the structure or exiting the application.

The ShowException function discussed later in this chapter is an example of an error-handling function for an OLE Automation Controller. Notice that the Invoke function frees the exception-info strings before it returns.