TCL TWAPI - Replacing strings in Word Document

80 Views Asked by At

I began using twapi for COM connections and like it very much so far. However, the documentation for interfacing of particular Office-packages is sort of weak (at least for me :) ) or lost over the years. My current problem is a very small one, but still, I'm stuck. (To say it just beforehand: pointing out any serious documentation for twapi usage would be really really helpful and appreciated)

I want to do a simple 'find and replace' in a Word-Document.

The VB code for this could look like this:

    Set myRange = ActiveDocument.Content 
    With myRange.Find 
     .ClearFormatting 
     .Replacement.ClearFormatting 
     .Text = "Hello" 
     .Replacement.Text = "Goodbye" 
     .Execute Replace:=wdReplaceAll 
    End With

Since it seems, that calling down the object tree in one shot is not possible in TCL, i tried this, to break it down to TCL:

    proc Word_FindAndReplace { doc  cSearch cReplace} { 

        try {

            set content [$doc Content]
            set find [$content Find]
            set repl [$find Replacement]
            $find ClearFormatting
            $repl ClearFormatting
            $find Text $cSearch
            $repl Text $cReplace
            #wdReplaceAll   2   ;#replace all
            $find Execute Replace 2   ;#   <-----not working
            return 1

        } on error {msg} {

            puts stderr "$msg $::errorInfo $::errorCode"
            return -1

        }

    }

The final "$find Execute Replace 2" is the culprit. Everything else just works fine. It seems that calling the "Execute" in the last line requires a special parameter format, but I don't know it. Any hint is highly appreciated.

1

There are 1 best solutions below

0
dietmar bos On

See comments please. The answer to the original question would still be good to know, since I suspect, it will come up in other places with twapi, but the current problem could be worked around. "Find" accepts all possible parameters at once (15 all in all), so the line

    $find Execute $cSearch 0 0 0 0 0 1 0 0 $cReplace 2 0 0 0 0 

solves my current problem.