FoxPress – Noviembre 2003

 

      Maneja OpenOffice desde Fox

 

 

Por Alberto Rodríguez con la colaboración de Carlos Guzman Alvarez

http://www.fpress.com/                                                                             

                                                                                                                         

 

    OpenOffice (OO) soporta la tecnología de Microsoft llamada Automation en todas las plataformas lo que permite que un cliente OLE como es el fox tome el control de Office externamente.

En el caso de OpenOffice todo tiene que empezar poniendo en marcha una instancia del Administrador de Servicios que se llama  Com.Sun.Star.ServiceManager

La forma de instanciarlo es con:

oSManager = CreateObject("Com.Sun.Star.ServiceManager.1")

El ServiceManager es el que nos permite acceder a los diferentes componentes del objeto. Para crear un Desktop usaremos:

   oSDesktop = oSManager.createInstance("com.sun.star.frame.Desktop")

Veamos un pequeño ejemplo que ya se publicó en esta revista hace tiempo:

LOCAL ARRAY laNoArgs[1]

LOCAL oSManager, oSDesktop, oStarDoc, oCursor

vbLf = Chr(10)

 

*- En caso de que no esté funcionando OO se pone en marcha

oSManager = CreateObject("Com.Sun.Star.ServiceManager.1")

 

*- Se crea un Desktop

oSDesktop = oSManager.createInstance("com.sun.star.frame.Desktop")

COMARRAY(oSDesktop, 10)

 

loReflection = oSManager.createInstance("com.sun.star.reflection.CoreReflection" )

COMARRAY( loReflection, 10 )

 

loPropertyValue = createStruct( @loReflection,"com.sun.star.beans.PropertyValue" )

laNoArgs[1] = loPropertyValue

laNoArgs[1].name = "ReadOnly"

laNoArgs[1].value = .F.

 

oStarDoc = oSDesktop.LoadComponentFromUrl("staroffice.factory:swriter", "_blank",0, @laNoargs)

oCursor = oStarDoc.Text.CreateTextCursor()

oStarDoc.Text.InsertString(oCursor, "Hola desde VFP" + vbLf , .F.)

oStarDoc.Text.insertString(ocursor, "Segunda Línea", .f.)

 

Function createStruct( toReflection, tcTypeName )

local loPropertyValue, loTemp

 loPropertyValue = createobject( "relation" )

toReflection.forName( tcTypeName).createobject(@loPropertyValue)

            return ( loPropertyValue )

endproc

 

Este ejemplo te abre el OpenOffice y te escribe un texto.

 

Un ejemplo más interesante sería que no te abra el OpenOffice y que el restultado te lo guarde en un documento con formato *.DOC

 

Para hacer esto, deberíamos hacer lo siguiente:

 

CLEAR all

 

LOCAL ARRAY laPropertyValue[1]

LOCAL loManager, loDesktop, loDocument, loCursor

loManager = CREATEOBJECT( "com.sun.star.ServiceManager" )

loDesktop = loManager.createInstance( "com.sun.star.frame.Desktop" )

comarray( loDesktop, 10 )

 

loReflection = loManager.createInstance("com.sun.star.reflection.CoreReflection" )

 

comarray( loReflection, 10 )

laPropertyValue[1] = createStruct( @loReflection,"com.sun.star.beans.PropertyValue" )

laPropertyValue[1].NAME = "Hidden"

laPropertyValue[1].VALUE = .T.

 

*!*

*!* Creamos un nuevo documento

*!*

 

loDocument = loDesktop.LoadComponentFromUrl( "private:factory/swriter","_blank", 0, @laPropertyValue )

comarray( loDocument, 10 )

loCursor       = loDocument.TEXT.CreateTextCursor()

loDocument.TEXT.InsertString( loCursor, "Hola desde VFP" , .F. )

 

*!*

*!* Salvamos el documento

*!*

 

laPropertyValue[1].NAME  = "FilterName"

laPropertyValue[1].VALUE = "MS Word 97"

 

loDocument.storeAsURL( "file:///c:/test.doc", @laPropertyValue )

 

*!*

*!* Terminamos la sesión en OpenOffice

*!*

 

loDocument.dispose()

 

loDesktop = .NULL.

loManager = .NULL.

loReflection = .NULL.

 

 

Function createStruct( toReflection, tcTypeName )

  local loPropertyValue, loTemp

  loPropertyValue = createobject( "relation" )

  toReflection.forName( tcTypeName).createobject(@loPropertyValue)

  return ( loPropertyValue )

endproc