Version 3 of Tcl & OpenOffice -translation from the French

Updated 2005-07-28 20:20:37

Translation Courtesy of Google

Open Office with Tcl and tcom

Miko

The office automation continuation Open Office constitutes a free alternative to Office of Microsoft. it can be controlled starting from Tcl with the extension tcom.

This code was tested on the following platform: Tcl 8.4, tcom 3.9 Open Office 1.1.4 and Windows XP. Under Windows NT4 SP6 with same the components, that does not function.

  package require tcom 
  set OO [::tcom::ref createobject "com.sun.star.ServiceManager" ]
  set OD [ $OO createInstance com.sun.star.frame.Desktop ]
  set Doc. [ $OD LoadComponentFromUrl private:factory/swriter _ blank 0 "" ]

this code functions with Tclsh, on the other hand once "packed" with Freewrap, that does not function more... "Explanation": the last argument of LoadComponentFromUrl is with Delphi a table of variable, initialized with a specific function... it was necessary to translate that into Tcl... Several tests (empirical, I recognize it) with list not having given anything, I had determined myself to employ Delphi for the part of my appealing program OO. Then while excavating on the Web I find this page: http://www.oooforum.org/forum/viewtopic.phtml?t=9815 With examples to control Open Office by the means of COM with various languages, of which Tcl. Besides the writer underlines the need for having already opened a document Open Office and it employs the following code:

  package require tcom 
  array set args { } 
  set objServiceManager [::tcom::ref createobject com.sun.star.ServiceManager ] 
  set Stardesktop [ $$objServiceManager createInstance com.sun.star.frame.Desktop ]
  # Because I was not correct whitebait to get the corresponding object standard for has standard UNO sequence I commented the loadComponentfromUrl out
  # set Doc. [  tardesktop LoadComponentfromUrl private:factory/swriter _ blank 0 "args" ]
  set Doc. [  tardesktop getCurrentComponent ]
  set xtext [ $doc text ] 
  $xtext setString { Hello World }

Oddly, you can note that it had the idea (judicious) to use "array set" for the last argument of LoadComponentfromUrl. The solution (tested with freewrap and the Calc spreadsheet of OO):

  set OO [::tcom::ref createobject "com.sun.star.ServiceManager" ] 
  set OD [ $OO createInstance com.sun.star.frame.Desktop ] 
  array set arguments { } 
  set Doc. [ $OD LoadComponentFromUrl private:factory/scalc _ blank 0 [ parray arguments ] ]

... it was necessary to use parray, which in the event does not return anything, but sometimes nothing equalizes rien.(bon, a null string does not agree identical to a array vacuum seen of the interior of Tcl, but this does not explain me why the first three lines of code function with tclsh and not with freewrap). "array get" could seem more judicious, but causes an error.

Lastly, now that the sorter is with the screen, one can acceder with his objects:

   set sheets [ $classeur Sheets [ expr 0 ] ] 
   if [ catch { set feuille1 [ $feuilles GetByName Sheet1 ] } ] { 
      set feuille1 [ $feuilles GetByName Feuille1 ] 
   }

Or (more portable)

  set feuille1 [ $classeur GetByIndex [ expr 0 ] ]

and to fill the cells (the value should be tested to be inserted because the access method is not the same one for the chains or the numbers)

  # the index of the cells is colonne/ligne 
  set cell [ $feuille1 getCellByPosition [ expr 0 ] [ expr 0 ] ] 
  $cellule SetString "Vive Tcl" 
  set cell [ $feuille1 getCellByPosition [ expr 1 ] [ expr 0 ] ] 
  $cellule SetValue 2005

It would be regretted almost that COM does not exist under Linux bus to control OO in the penguin there should be done in Java...

Updated on May 30, 2005 to 08:51 GMT - History - To publish Open Office with Tcl and tcom