Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Excel add in development in C++ Aplications in finance - Dalton S.

Dalton S. Excel add in development in C++ Aplications in finance - Wiley publishing , 2005. - 425 p.
ISBN 0-470-02469-0
Download (direct link): exceladdindevelopmentincand2005.pdf
Previous << 1 .. 68 69 70 71 72 73 < 74 > 75 76 77 78 79 80 .. 168 >> Next

The first point is very straightforward. The enumerated function constant, for example xlcDefineName, should be bit-wise or’d with the value 0x10 0 0, defined as xlPrompt in the SDK header file.
On the second point, the arguments supplied pre-populate the fields in the dialog box. Any that are not supplied will result in either blank fields or fields that contain Excel defaults.
Any command function that can be called in this way will return true if successful and false if cancelled or unsuccessful.
For example, the following command calls the xlcDefineName function with the dialog displayed.
int stdcall define_new_name(void)
{
// Get the name to be defined from the active cell. First get a // reference to the active cell. No need to evaluate it, as call // to xlcDefineName will try to convert contents of cell to a // string and use that.
cpp_xloper Name;
int xl4 = Excel4(xlfActiveCell, &Name, 0); Name.SetExceltoFree();
if(!xl4 && !Name.IsType(xltypeErr))
Excel4(xlcDefineName | xlPrompt, 0, 1, &Name);
return 1;
}
8.2 THE Excel4() C API FUNCTION
8.2.1 Introduction
Once inside the DLL you will sometimes need or want to call back into Excel to access its functionality. This might be because you want to take advantage of Excel’s ability
172
Excel Add-in Development in C/C++
to convert from one data type to another (especially where the input might be one of a number of things that Excel has passed to you as an argument to a function), or because you need to register or un-register a DLL function or free some memory that Excel has allocated. Excel provides two functions that enable you to do all these things, Excel4() and Excel4v(). These are essentially the same function, the first taking a variable argument list, the second fixed but with a variable sized array of arguments that you wish to pass in.
The syntax for Excel4() is:
int Excel4(int xlfn, xloper *RetVal, int count, ...);
Note that the calling convention is cdecl in order to support the variable argument list.
Here is a brief overview of the arguments:
Table 8.2 Excel4() arguments
Argument Meaning Comments
int xlfn A number corresponding to a function or command recognised by Excel as part of the C API. Must be one of the predefined constants defined in the SDK header file xlcal 1. h
xloper *pRetVal A pointer to an xloper that will contain the return value of the function xlfn if Excel4 ( ) was able to call it. If a return value is not required by the caller. NULL (zero) can be passed. If Excel4 () was unable to call the function, the contents of this are unchanged. Excel allocates memory for certain return types. It is the responsibility of the caller to know when and how to tell Excel to free this memory. (See xlFree and xlbitXLFree.) If a function does not return an argument, for example. xlFree, Excel4 () will ignore pRetval.
int count The number of arguments to xl fn being passed in this call to Excel4 ( ) . The maximum value is 30.
xloper *argl A pointer to an xlopers containing the arguments for xlfn. Missing arguments should be passed as xlopers of type xltypeMissing.

xloper *arg30
Accessing Excel Functionality Using the C API
173
The xlfn function being executed will always be one of the following:
• an Excel worksheet function;
• a C API-only function;
• an Excel macro sheet function;
• an Excel macro sheet command function.
These function enumerations are defined in the SDK header file xlcall.h as either xlf- or xlc-prefixed depending on whether they are functions or commands. There are also a number of non-XLM functions available only to the C API, such as xlFree.
The following sections provide more detail.
8.2.2 Excel4() return values
The value that Excel4() returns reflects whether the supplied function (designated by the xlfn argument) was able to be executed or not. If successful Excel4() returns zero (defined as xlretSuccess), BUT this does not always mean that the xlfn function executed without error. To determine this you need to check the return value of the xlfn function passed back via the xloper *pRetVal. Where Excel4() returns a non-zero error value (see below for more details) you do know that the xlfn function was either not called at all or did not complete.
The return value is always one of the values given in Table 8.3. (Constants in parentheses are defined in the SDK header file xlcall.h.)
Table 8.3 Excel4() return values
Returned value Meaning
0 (xlretSuccess) The xlfn function was called successfully, but you need also to check the type and/or value of the return xloper in case the function could not perform the intended task.
1 (xlretAbort) The function was called as part of a call to a macro that has been halted by the user or the system.
2 (xlretlnvXlfn) The xl f n function is not recognised or not supported or cannot be called in the given context.
4 (xlretlnvCount) The number of arguments supplied is not valid for the specified xlfn function.
8 (xlretlnvXloper) One or more of the passed-in xlopers is not valid.
Previous << 1 .. 68 69 70 71 72 73 < 74 > 75 76 77 78 79 80 .. 168 >> Next