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 En este caso hemos usado un filtro para decirle que nos lo guarde en formato Word. Si quisieramos que nos lo guardara en PDF, debereiamos 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] = createStruct( @loReflection,"com.sun.star.beans.PropertyValue" ) laPropertyValue[1].NAME = "FilterName" laPropertyValue[1].VALUE = "impress_pdf_Export" laPropertyValue[1].NAME = "CompressionMode" laPropertyValue[1].VALUE = 1 laPropertyValue[1].NAME = "Pages" laPropertyValue[1].VALUE = "ALL" loDocument.storeToURL( "file:///c:/test.pdf", @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 El código es muy parecido al anterior pero con el filtro correspondiente al PDF. Como seguramente sabras, OpenOffice te permite guardar los documentos en PDF con lo que tienes una ventaja añadida sobre el Word de Microsoft. Un ejemplo con más opciones es el siguiente que es la traducción de un ejemplo para VB que proponen en el API de OpenOffice. 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 = "ReadOnlu" laPropertyValue[1].VALUE = .T. *!* *!* Creamos un nuevo documento *!* loDocument = loDesktop.LoadComponentFromUrl( "private:factory/swriter","_blank", 0, @laPropertyValue ) comarray( loDocument, 10 ) loCursor = loDocument.TEXT.CreateTextCursor() *'Inserting some Text loDocument.text.insertString(loCursor, "The first line in the newly created text document.", .f.) *'Inserting a second line loCursor.text.insertString(loCursor, "Now we're in the second line", .f.) *'Create instance of a text table with 4 columns and 4 rows objTable= loDocument.createInstance( "com.sun.star.text.TextTable") objTable.initialize(4, 4) *'Insert the table loCursor.text.insertTextContent(loCursor, objTable, .f.) *'Get first row objRows= objTable.getRows objRow= objRows.getByIndex( 0) *'Set the table background color objTable.setPropertyValue("BackTransparent", .f.) objTable.setPropertyValue("BackColor", 13421823) *'Set a different background color for the first row objRow.setPropertyValue( "BackTransparent", .f.) objRow.setPropertyValue( "BackColor", 6710932) *'Fill the first table row insertIntoCell ("A1","FirstColumn", objTable ) insertIntoCell ("B1","SecondColumn", objTable ) insertIntoCell ("C1","ThirdColumn", objTable ) insertIntoCell ("D1","SUM", objTable ) objTable.getCellByName("A2").setValue(22.5) objTable.getCellByName("B2").setValue(5615.3) objTable.getCellByName("C2").setValue(-2315.7) objTable.getCellByName("D2").setFormula ("sum ") objTable.getCellByName("A3").setValue(21.5) objTable.getCellByName("B3").setValue(615.3) objTable.getCellByName("C3").setValue(-315.7) objTable.getCellByName("D3").setFormula ("sum ") objTable.getCellByName("A4").setValue (121.5) objTable.getCellByName("B4").setValue (-615.3) objTable.getCellByName("C4").setValue (415.7) objTable.getCellByName("D4").setFormula("sum ") *'Change the CharColor and add a Shadow loCursor.setPropertyValue ("CharColor", 255) loCursor.setPropertyValue ("CharShadowed", .t.) *'Create a paragraph break *'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant loCursor.text.insertControlCharacter (loCursor, 0 , .f.) *'Inserting colored Text. loDocument.text.insertString (loCursor, " This is a colored Text - blue with shadow" , .f.) *'Create a paragraph break ( ControlCharacter::PARAGRAPH_BREAK). loDocument.text.insertControlCharacter( loCursor, 0, .f.) *'Create a TextFrame. loDocumentFrame= loDocument.createInstance("com.sun.star.text.TextFrame") *'Create a Size struct. objSize= createStruct(@loReflection, "com.sun.star.awt.Size") objSize.Width= 15000 objSize.Height= 400 loDocumentFrame.setSize( objSize) *' TextContentAnchorType.AS_CHARACTER = 1 loDocumentFrame.setPropertyValue("AnchorType", 1) *'insert the frame loDocument.text.insertTextContent(loCursor, loDocumentFrame, .f.) *'Get the text object of the frame objFrameText= loDocumentFrame.getText *'Create a cursor object objFrameTextCursor= objFrameText.createTextCursor *'Inserting some Text objFrameText.insertString(objFrameTextCursor, "The first line in the newly created text frame.", .f.) objFrameText.insertString(objFrameTextCursor, "With this second line the height of the frame raises.", .f.) *'Create a paragraph break *'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant objFrameText.insertControlCharacter(loCursor, 0 , .f.) *'Change the CharColor and add a Shadow loCursor.setPropertyValue ("CharColor", 65536) loCursor.setPropertyValue ("CharShadowed", .f.) *'Insert another string loDocument.text.insertString (loCursor, " That's all for now !!", .f.) Function insertIntoCell( strCellName, strText, objTable) objCellText= objTable.getCellByName( strCellName) objCellCursor= objCellText.createTextCursor objCellCursor.setPropertyValue ("CharColor",16777215) objCellText.insertString(objCellCursor, strText, .f.) EndProc Function createStruct( toReflection, tcTypeName ) local loPropertyValue, loTemp loPropertyValue = createobject( "relation" ) toReflection.forName( tcTypeName).createobject(@loPropertyValue) return ( loPropertyValue ) endproc FoxPress – Noviembre de 2003 © 2003 FoxPress. All rights reserved