COM Type-Cast Conversions
If any of variable data type declaration suffixes is used or if missing and variable's type is found , COM parser will automatically convert VARIANTS to and from that data type.
Variable data type declaration suffix
The data type of a variable can be indicated by a data type declaration suffix (%, !, # or $) appended to the variable name.
DIM A%indicates that A is an integer variable.
DIM B!indicates that B is a single precision float variable.
DIM C#indicates that C is a double precision float variable.
DIM D$indicates that D is string variable.
Samples:
' parser will pass string variable and automatically convert to BSTR cdonts.from = D$
' Parser will pass Integer variable. app.activeworkbook.cells(1,3).value = A%
' Parser will pass float variable. app.activeworkbook.cells(1,3).value = B!
If no type is specified, an attempt is made to find the variable's type and, if found, it is used. Otherwise, the BCX COM parser will pass a long as the default data type.
The BCX COM parser uses similar syntax for type-casting as the C language. The only difference is that all casts are always from and to the VARIANT data type and casting may occur to and from one of the constants defined in table below.
' Parser will convert integer A% to double precision float. app.activeworkbook.cells(1,3).value = (VT_R8) A%
' result of GetCom method will be converted to float. B! = (VT_R4) app.activeworkbook.cells(4,3).value
If the return type is not known then you may use a varible of type VARIANT and check the .vt for what type is returned. For example,
DIM vRet AS VARIANT vRet = app.activeworkbook.cells(1,3).value IF vRet.vt = VT_R8 THEN ' do something if it's a double ' Access val with vRet.dblval ELSEIF vRet.vt = VT_BSTR ' do something if it's a BSTR (note: BSTR is a wide strings) ' Access val with vRet.bstrVal END IF
👉 some COM calls return different types.
List of available conversion definitions:
| Valid casts (VT_...) | C/C++ equivalent | member |
| VT_NONE | void | void |
| VT_UI1 | unsigned char | bVal |
| VT_UI1REF | unsigned char* | by reference |
| VT_I2 | SHORT | iVal |
| VT_I2REF | SHORT* | by reference |
| VT_I4 | long | lVal |
| VT_I4REF | long* | by reference |
| VT_R4 | float | fltVal |
| VT_R4REF | float* | by reference |
| VT_R8 | double | dblVal |
| VT_R8REF | double* | by reference |
| VT_UI8 | unsigned __int64 | uhVal |
| VT_UI8REF | unsigned __int64* | by reference |
| VT_I1 | char | bVal |
| VT_I1REF | char* | by reference |
| VT_UI2 | USHORT | uiVal |
| VT_UI2REF | USHORT* | by reference |
| VT_UI4 | ULONG | ulVal |
| VT_UI4REF | ULONG* | by reference |
| VT_INT | int | lVal |
| VT_INTREF | int* | by reference |
| VT_UINT | UINT | ulVal |
| VT_UINTREF | UINT* | by reference |
| VT_CY | CY | cyVal |
| VT_CYREF | CY* | by reference |
| VT_DATE | DATE | date |
| VT_DATEREF | DATE* | by reference |
| VT_BSTR | BSTR | bstrVal |
| VT_BSTRREF | BSTR* | by reference |
| VT_DISPATCH | IDispatch* | pdispVal |
| VT_DISPATCHREF | IDispatch** | by reference |
| VT_ERROR | SCODE | scode |
| VT_ERRORREF | SCODE* | by reference |
| VT_BOOL | VARIANT_BOOL | boolVal |
| VT_BOOLREF | VARIANT_BOOL | by reference |
| VT_UNKNOWN | IUnknown* | punkVal |
| VT_UNKNOWNREF | IUnknown** | by reference |
| VT_VARIANTREF | VARIANT* | by reference |
| VT_ARRAY | SAFEARRAY | parray |
| VT_ARRAYREF | SAFEARRAY* | by reference |
| VT_DECIMAL | DECIMAL | decVal |
| VT_DECIMALREF | DECIMAL* | by reference |
Related topics: Object data type definition | List of all COM Interface Functions