Skip to main content

Problem:

Launch the Run method of the COM WORD application component.

in the COBOL sample pasted  below, the cobol program opens a file test.doc ( located on the current directory )  which contains a macro named MaMacro which receives a String parameter:

Sub MaMacro(machaine As String)

    MsgBox machaine

End Sub

Resolution:

      *> This is required for OLE applications

      $set ooctrl( P)

       program-id. worddemo.

       class-control.

       *> OLE automation classes

           wordapp is class "$OLE$word.application"

           .

       working-storage section.

       01 wordServer       object reference value null.

       01 theDocuments     object reference value null.

       78 FileName        value     z"\\test.doc".

       01 flags             pic x(4) comp-5 value 0.

       01 name-length       pic x(4) comp-5.

       01 directory-Name    pic x(52).

       01 Status-Code       pic x(4) comp-5.

       01 PathFileName      pic x(100).

       copy "mfole.cpy".               *> Variant record and types

       01 MethodVar32         VARIANT.

       01 MacroName           pic x(100) value z"MaMacro".

       procedure division.

           *> Create an instance of Word

           invoke wordapp "new" returning wordServer

           *> Make it visible

           invoke wordServer "SetVisible" using by value  1

           *> Get the Word documents collection object

           invoke wordServer  "getDocuments" returning theDocuments

           move length of directory-name to name-length

           call "CBL_GET_CURRENT_DIR" using by value flags

                                      by value       name-length

                                      by reference   directory-name

                                      returning      status-code

           end-call

           if status-code not = 0

                    display "error CBL_GET_CURRENT_DIR "

                    accept directory-name with timeout 0

                    STOP RUN

           else

                    string directory-name    delimited by space

                    FileName

                    delimited by size into PathFileName

           end-if

           invoke theDocuments  "Open"

                       using by reference  PathFileName

                       returning theDocuments

           invoke  wordServer "Run"

                                   using by reference  MacroName

                                         by reference  PathFileName       *> parameter passed to macro

                                   returning MethodVar32

           *> We no longer need the Documents collection

           invoke theDocuments "finalize" returning theDocuments

           *> We've now finished with the Word object

           invoke wordServer "finalize" returning wordServer

           exit program

           stop run.

Old KB# 6997