Sample 23 is a program that uses the COBOL COM client function to operate Excel.
The COBOL COM client function creates a COM object that uses functions which the COM server supports in the same way as a COBOL method.
Excel is an independent application and also functions as a COM server. Therefore, an application that operates Excel can be written with COBOL.
To run this program, the following products are necessary:
Microsoft(R) Excel
"Excel" is used here as a generic name for these products.
For details on the COBOL COM function, refer to Chapter 24 in the "NetCOBOL User’s Guide".
Overview
The sample program performs the operations listed below for Excel from a COBOL application.
Excel activation and termination
Excel sheet open
Work sheet selection and data insertion
Graph creation
Printing of a selected sheet
The COBOL COM client function supports the early binding and late binding methods to recognize COM server methods and interfaces.
Late binding is used in this sample.
When late binding is used, usable COBOL descriptions are limited and execution performance is slower than early binding, but late binding is freer from the effects of COM server modifications than early binding. When using late binding this program can operate Excel97 and Excel2000, which are different COM servers.
For details of early binding, see Sample 24, "COM Program to Control Excel (Early Binding)."
Available Programs
SAMPLE23.PRJ (Project file)
SAMPLE23.COB (COBOL source file)
GRAPHDATA.XLS (Excel file for test)
Available COBOL Functions
COM client function
Applicable COBOL Statements
The DISPLAY, IF, INVOKE, PERFORM, and SET statements are used.
Building/Rebuilding
The Project Manager's Build function is used to compile and link this program.
Folder C:\NetCOBOL is assumed below as the location where NetCOBOL is installed.
Change the folder name C:\NetCOBOL to the name of the folder where NetCOBOL is installed.
Start the Project Manager.
Open the project file SAMPLE23.PRJ.
Select "Build" on the "Project" menu.
After the build terminates, check that SAMPLE23.EXE is created.
Executing the Program
Select "Execute" on the "Project" menu.
Excel is started. The following message is also displayed on the COBOL console and the system waits for input.
Use the full path-name to specify an Excel file for the test.
The specified Excel file is opened and the sample program controls Excel.
The following message is displayed on the COBOL console and the system waits for input again.
Input the type of graph to be created.
The specified graph is plotted. The following message is displayed on the COBOL console and the system waits for input again.
If the graph is to be printed, specify a printer name. If it is not to be printed, press the ENTER key without inputting data.
Excel is terminated and the program is terminated. The graph is printed before terminating Excel when a printer name is specified.