Created On:  8 June 2010

Problem:

OpenOffice is a suite of application tools that provide a freeware alternative to using Microsoft Office.
The Calc application is the alternative to Excel.
OpenOffice has a COM interface but it is not compatible with the COM interface provided by Microsoft Office.

A sample is needed to show how to access and manipulate a Calc spreadsheet using COM from a Net Express client so that the spreadsheet is saved as an Excel compatible format.

Resolution:

The following is a sample Net Express program which will create a Open Office Calc spreadsheet using the COM interface and then save the spreadsheet as an Excel compatible document:

      $set sourceformat(variable)                                                                    
      $set ooctrl( P)                                                                                
       IDENTIFICATION DIVISION.                                                                      
       ENVIRONMENT DIVISION.                                                                         
                                                                                                     
       class-control.                                                                                
           OServManager is class "$OLE$com.sun.star.ServiceManager"                                  
           oleSafeArray is class "olesafea"  *> OLE SafeArray class                                  
           olesup is class "olesup"                                                                  
           OLEVariant is class "olevar"                                                              
            olebase is class "olebase"                                                               
           .                                                                                         
                                                                                                     
       DATA DIVISION.                                                                                
        WORKING-STORAGE SECTION.                                                                     
                                                                                                     
       copy mfole.cpy.                                                                               
                                                                                                     
       copy olesafea.cpy.            *> SafeArray records                                            
                                                                                                     
       01 theSafeArrayObj  object reference.                                                         
                                                                                                     
       01 saBound          SAFEARRAYBOUND.                                                           
                                                                                                     
       01 ServManager          object reference.                                                     
       01 StarDesktop          object reference.                                                     
       01 aDoc object reference.                                                                     
       01 aSheets object reference.                                                                  
       01 aSheet object reference.                                                                   
       01 aCell object reference.                                                                    
       01 aTempObj object reference.                                                                 
       01 oPropertyValue  object reference.                                                          
       01 hResult            pic 9(9) comp-5.                                                        
       01 iIndex               pic 9(9) comp-5 value 0.                                              
           PROCEDURE DIVISION.                                                                       
                                                                                                     
           invoke OServManager "new" returning ServManager                                           
                                                                                                     
           invoke ServManager "createInstance" using "com.sun.star.frame.Desktop" returning StarDeskto
                                                                                                     
           move 0 to llBound   of saBound                                                            
           move 1 to cElements of saBound                                                            
           invoke olesafearray "new" using                                                           
               by value VT-VARIANT                                                                   
               by value 1                  *> Single dimension                                       
               by reference saBound        *> Dimension boundaries                                   
               returning theSafeArrayObj                                                             
                                                                                                     
           invoke StarDesktop "loadComponentFromURL" using                                           
               by reference "private:factory/scalc"                                                  
               by reference "_blank"                                                                 
               by value 0                                                                            
               by reference theSafeArrayObj                                                          
               returning aDoc                                                                        
                                                                                                     
           invoke aDoc "getSheets" returning aSheets                                                 
                                                                                                     
           invoke olesup "setDispatchType" using by value 0   *> value 0 = next message forced to invoke
           invoke aSheets "getByName" using by reference "Sheet1" returning aSheet                    
                                                                                                      
           invoke olesup "setDispatchType" using by value 0   *> value 0 = next message forced to invoke
           invoke aSheet "getCellByPosition" using by value 0, by value 0 returning aCell            
           invoke aCell "SetString" using "Hello World"                                               
                                                                                                      
           invoke ServManager "Bridge_GetStruct" using "com.sun.star.beans.PropertyValue" returning oP
                                                                                                     
           invoke olesup "setDispatchType" using by value 1   *> value 1 = next message forced to invoke
           invoke oPropertyValue "Name" using z"FilterName"                                          
                                                                                                     
           invoke olesup "setDispatchType" using by value 1   *> value 1 = next message forced to invoke
           invoke oPropertyValue "Value" using z"MS Excel 97"                                        
           invoke theSafeArrayObj "putOLEObject"                                                     
              using Iindex                                                                           
                    by value oPropertyValue                                                          
              returning hResult                                                                      
           end-invoke.                                                                                
           invoke aDoc "storeAsURL" using                                                            
               by reference z"file:///C:/cobol/calctstx.xls"                 
               by reference theSafeArrayObj                                                           
                                                                                                      
      *>   Finalize all objects                                                                      
           invoke aCell "finalize" returning aCell                                                   
           invoke aSheet "finalize" returning aSheet                                                 
           invoke aSheets "finalize" returning aSheets                                               
                                                                                                     
           invoke aDoc "Close" using by value -1                                                      
           invoke aDoc "Finalize" returning aDoc                                                     
           invoke StarDesktop "Finalize" returning StarDesktop                                       
           invoke ServManager "Finalize" returning ServManager                                       
                                                                                                     
           goback.                                                                                   
Incident #2288367