Example: Spreadsheet Control
This example uses an ActiveX control that displays a spreadsheet interface. The control, contained in the file msowc.dll, is installed along with a typical installation of Microsoft Office. If the control is not present on your system (you'll know the control is not present if the example code fails when trying to realize the widget hierarchy), the example will not run.
The spreadsheet control has the program ID:
and the class ID:
{0002E510-0000-0000-C000-000000000046}
Note
The spreadsheet control used in this example is included with older versions of Microsoft Office; it is discussed in Microsoft Knowledge Base Article 248822. Newer versions of Microsoft Office may include spreadsheet controls with updated program and class IDs.
Information about the spreadsheet control's properties and methods was gleaned from Microsoft Excel 97 Visual Basic Step by Step by Reed Jacobson (Microsoft Press, 1997) and by inspection of the control's interface using the OLE/COM Object Viewer application provided by Microsoft. It is beyond the scope of this manual to describe the spreadsheet control's interface in detail.
Example Code
This example, ActiveXExcel.pro, is included in the examples\doc\bridges\COM subdirectory of the IDL distribution and develops an IDL routine called ActiveXExcel that illustrates use of the spreadsheet ActiveX control within an IDL widget hierarchy. Run the example procedure by entering ActiveXExcel at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT ActiveXExcel.pro.
- Create a function that will retrieve data from cells selected in the spreadsheet control. The function takes two arguments: an object reference to the IDLcomActiveX object that instantiates the spreadsheet control, and a variable to contain the data from the selected cells.
- Retrieve an object that represents the selected cells. Note that when the ActiveX control returns this object, IDL automatically creates an IDLcomActiveX object that makes it accessible within IDL.
- Retrieve the total number of cells selected.
- If no cells are selected, destroy the selection object and return zero (the failure code).
- Retrieve objects that represent the dimensions of the selection.
- Get the dimensions of the selection, then destroy the column and row objects.
- Create a floating point array with the same dimensions as the selection.
- Iterate through the cells, doing the following:
- Destroy the selection object.
- Return one (the success code) and end the function definition.
- Next, create a procedure that sets the values of the cells in the spreadsheet. This procedure takes one argument: an object reference to the IDLcomActiveX object that instantiates the spreadsheet control.
- Define the size of the data array.
- Get an object representing the active spreadsheet.
- Get an object representing the cells in the spreadsheet.
- Generate some data.
- Iterate through the elements of the data array, doing the following:
- Destroy the spreadsheet and cell objects, and end the procedure.
- Next, create a procedure to handle events generated by the widget application.
- The user value of the top-level base widget is set equal to a structure that contains the widget ID of the ActiveX widget. Retrieve the structure into the variable sState.
- Use the value of the DISPID field of the event structure to sort out "selection changing" events.
- Place data from selected cells in variable aData, using the
excel_getSelectionfunction defined above. Check to make sure that the function returns a success value (one) before proceeding. - Get the dimensions of the aData variable.
- If aData is two-dimensional, display a surface, otherwise, plot the data.
- Reset the state variable sState and end the procedure.
- Create the main widget creation routine.
- Create a top-level base widget.
- Instantiate the ActiveX spreadsheet control in a widget.
- Realize the widget hierarchy.
- The value of an ActiveX widget is an object reference to the IDLcomActiveX object that encapsulates the ActiveX control. Retrieve the object reference in the variable oExcel.
- Turn off the TitleBar property on the spreadsheet control.
- Populate the spreadsheet control with data, using the
excel_setDatafunction defined above. - Set the user value of the top-level base widget to an anonymous structure that contains the widget ID of the spreadsheet ActiveX widget.
- Call XMANAGER to manage the widgets, and end the procedure.
oCols->GetProperty, COUNT=nCols OBJ_DESTROY, oCols oRows->GetProperty, COUNT=nRows OBJ_DESTROY, oRows
IF (szData[0] GT 1 AND szData[1] GT 1 AND szData[2] GT 1) $ THEN SURFACE, aData $ ELSE $ PLOT, aData ENDIF ENDIF
wAx=WIDGET_ACTIVEX(wBase, $ '{0002E510-0000-0000-C000-000000000046}', $ SCR_XSIZE=600, SCR_YSIZE=400)
Running the ActiveXExcel procedure display widgets that look like the following:
