OLE Automation for Interfacing with Excel

0 Likes

This article describes how to use OLE automation to interact with Excel from COBOL.

Problem:

Exporting data from COBOL and loading into Excel is possible by simply writing to a line sequential file with comma separated values and then performing a CALL “SYSTEM”. However, a more elegant and powerful solution is to use the OLE automation interface.

Resolution:

Please see the attached ACUCOBOL-GT v8.1 example included in the excel-ole.zip file. In essence you can do the following:

       77 excel-app       handle of application.
       77 excel-doc       handle of workbook.
       77 excel-sheets    handle of sheets.
       77 excel-worksheet handle of worksheet.
       77 excel-range     handle of range.

      ******************************************************************
       PROCEDURE DIVISION.
      
       Main Section.
     
      * Create the OLE interface
           create @Application of @Excel handle in excel-app.
                          
      * Don't show it yet!
           modify  excel-app @Visible = 0.

      * Use this to create a new excel doc
           modify excel-app
           @Workbooks::Add()
              giving excel-doc.
        
      * Use this code block to open an existing excel doc
      *     modify  excel-app
      *     @Workbooks::Open("c:\temp\excel-ole\excel.xlsx")
      *       giving excel-doc.
     
      * Get the worksheets collection
           inquire excel-doc sheets() in excel-sheets.

      * Get the first worksheet
           inquire excel-sheets item(1) in excel-worksheet.

      * Get some cells
           inquire excel-worksheet cells in excel-range.

      * Lets write something to it
           modify excel-range Item(1, 1, "Hello").
           modify excel-range Item(1, 2, "Cruel").
           modify excel-range Item(1, 3, "World").

      * Display it      
           modify excel-app @Visible = 1.

      * Clean up
           destroy excel-doc.
           destroy excel-app.
           destroy excel-sheets.
           destroy excel-worksheet
           destroy excel-range

      * Get me out of here...
           Exit Program.
           Stop Run.         

Incident Number: 2267067

Attachments

Old KB# 14345
Comment List
Anonymous
Related Discussions
Recommended