Page 1 of 1

LibreOffice Document, replace String

Posted: Fri Oct 04, 2019 7:04 pm
by Jorge_T
Hola,

Abro un documento Libreoffice "odt" para sustituir una cadena de texto para luego guardar el documento modificado con otro nombre... en principio funciona, pero me gustaria hacerlo en modo "oculto", es decir, que no me muestre como se abre y cierra el LibreOffice.

Este es mi programa:

function Main()

cfile1="c:\doc\carta1.odt" // este documento contiene el texto "saludos"
cfile2="c:\doc\newdoc.odt" // en el nuevo documento el texto sera "atentamente"
curlf1="file:///"+strtran(cfile1,"\","/")
curlf2="file:///"+strtran(cfile2,"\","/")

oService := CreateObject( "com.sun.star.ServiceManager" )
oDesktop := oService:CreateInstance( "com.sun.star.frame.Desktop" )

aProp:={}
oDoc := oDesktop:LoadComponentFromURL( curlf1, "_blank", 0, aProp ) //abre el fichero 1

oDispatcher:= oService:CreateInstance( "com.sun.star.frame.DispatchHelper" )

AAdd(aProp,GetPropertyValue(oService, "SearchItem.SearchString" , "saludos") ) //busca cadena
AAdd(aProp,GetPropertyValue(oService, "SearchItem.ReplaceString", "atentamente" ) ) //reemplaza cadena
AAdd(aProp,GetPropertyValue(oService, "SearchItem.Command" , 3 ) )
AAdd(aProp,GetPropertyValue(oService, "Hidden" , .T. ) )
oDispatcher:executeDispatch(oDoc:GetCurrentController():GetFrame(), ".uno:ExecuteSearch", "", 0, aProp)

AAdd(aProp,GetPropertyValue(oService, "URL", curlf2) ) // Fichero de salida
AAdd(aProp,GetPropertyValue(oService, "FilterName", "writer8") )
oDispatcher:executeDispatch(oDoc:GetCurrentController():GetFrame(), ".uno:SaveAs", "", 0, aProp)

odoc:dispose() //cierra el fichero

return
//-----------------------------------------------------//
STATIC FUNCTION GetPropertyValue(oService, cName, xValue )
LOCAL oArg
oArg := oService:Bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
oArg:Name := cName
oArg:Value := xValue
RETURN oArg