Calling Macro Language Functions from DLLs

The "Calling the Function from Within Microsoft Excel" section, on page 147, discussed using REGISTER and CALL to call DLL functions from the macro language. You can also call macro language functions from DLLs.

You can use xlUDF to call user-defined functions, that is, functions defined in a macro sheet or add-in. For the first argument to xlUDF, use a reference to the function you want to execute. Then pass all the arguments to the function you are calling. For example, if you had a user-defined function named FUNC, you could call FUNC(5) as follows:

XLOPER xFuncStr, xFuncRef, x5, xResult;

xFuncStr.xltype = xltypeStr;
xFuncStr.val.str = "\004FUNC";

x5.xltype = xltypeNum;
x5.val.num = 5;

/*
** Lookup the name FUNC using EVALUATE
*/

Excel4(xlfEvaluate, &xFuncRef, 1, (LPXLOPER) &xFuncStr);
Excel4(xlUDF, &xResult, 2, (LPXLOPER) &xFuncRef, (LPXLOPER) &x5);

/*
** After using xResult, don't forget to free it:
*/

Excel4(xlFree, &xResult, 2, (LPXLOPER) &xResult, (LPXLOPER) &xFuncRef);