Calling Microsoft Excel from C

Earlier in this chapter, you learned how to call DLLs from Microsoft Excel. However, in the CIRCUM example, you didn't actually drive Microsoft Excel; you just calculated a function and returned the result. Starting with Microsoft Excel 4.0, it is possible to call internal Microsoft Excel functions directly from the code in your DLL.

Note

The examples in this chapter discuss using the C API to call internal Microsoft Excel functions. For information about using the OLE Automation interface to access Microsoft Excel Visual Basic objects, methods, and properties, see Chapter 4, "Using OLE Automation."

Driving Microsoft Excel from C is straightforward because there is only one function, Excel4, to learn. The following is the function prototype:

int _cdecl Excel4(int iFunction, LPXLOPER pxRes, int iCount, ...)

The Excel4 function takes three or more arguments:

The third argument is followed by the actual arguments to the Microsoft Excel function. All the arguments to Microsoft Excel functions and their return values must always be specified as pointers to XLOPER data structures, which are discussed in the following section. The return value indicates success or an error code. The keyword _cdecl indicates that the function uses the C calling convention.

To use this function, you need to know the following:

The next two sections discuss these two issues. Then you will be ready to start programming Microsoft Excel in C.