Skip to main content

Problem:

Release 5.0:

Using COM Automation with Microsoft Word then how to perform a find and replace in the documents.

Resolution:

The work demo supplied with Net Express V5 in the COMDEMOS directory contains an example of this in Word5.cbl.

The code that performs a find and replace is:-

           invoke theDocument "getParagraphs" returning theParagraphs

           invoke theParagraphs "item" using by value 2

                               returning theParagraph

           invoke theParagraph "getRange" returning theRange

           invoke theParagraph "finalize" returning theParagraph

           invoke theParagraphs "finalize" returning theParagraphs

           *> Get the Find object and ask it to find the text "third"

           *> This should change theRange object's character range

           invoke theRange "getFind" returning theFind

           invoke theFind "setForward" using by value 1

           invoke theFind "setText" using by content z"third"

           invoke theFind "execute"

           invoke theFind "getFound" returning found

           invoke theFind "finalize" returning theFind

           if found not = 0

               *> Select our new range of characters

               invoke theRange "select"

               *> Finally, change the word to the correct word!

               invoke theRange "setText" using "second"

           end-if

           invoke theRange "finalize" returning theRange

Old KB# 1272