FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Open/Save document with OpenOffice Writer
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Open/Save document with OpenOffice Writer
Posted: Mon Mar 16, 2009 10:57 AM
Hello,

Does anyone have a example how to open , create a new document and save / save as a document with openoffice write with FWH?


For MSWord I use something like
Code (fw): Select all Collapse
      nHandle:= GetModuleHandle("WinWord.exe")

      IF nHandle = 0

         nStatus := WinExec( LFN2SFN(mfindexe(cFile)), 3, 9 )
      ELSE
         hWnd := FINDWINDOW( , "Microsoft Word" )

         IF hWnd > 0
            ShowWindow( hWnd, 3 )
         ENDIF

      ENDIF
      oDDE := TDDEMLClient():new()
      oDDE:Connect("WINWORD","SYSTEM")

      oDDE:Execute( '[AppActivate "Microsoft Word", 1]' )
      oDDE:Execute( '[FileOpen.Name:="' + cFile + '"]' )



Thanks,
Marc
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Open/Save document with OpenOffice Writer
Posted: Mon Mar 16, 2009 11:55 AM
Marc,

Try this for a start

Code (fw): Select all Collapse
oService   := CreateObject( "com.sun.star.ServiceManager" )
oDesktop := oService:CreateInstance( "com.sun.star.frame.Desktop" )

// Opens a Blank Document
aProp:={}
oDoc    := oDesktop:LoadComponentFromURL( "private:factory/swriter", "_blank", 0, aProp )


Regards

Anser
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Open/Save document with OpenOffice Writer
Posted: Mon Mar 16, 2009 01:30 PM

Thanks Anser,

That's working fine. Do you also know the commands to to open an existing document and to save(as)?

Regards,
Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Open/Save document with OpenOffice Writer
Posted: Mon Mar 16, 2009 04:39 PM
Dear Marc,

I think it is like the following. I am not sure

Code (fw): Select all Collapse
oService   := CreateObject( "com.sun.star.ServiceManager" )
oDesktop := oService:CreateInstance( "com.sun.star.frame.Desktop" )

// Opens a specific Document
aProp:={}
oDoc    := oDesktop:LoadComponentFromURL( "file:///C:/MyTest/Samples/Resume.sxw", "_blank", 0, aProp )

// Insert sample Text
oText:=oDoc:getText() // Create text object

// Create a cursor object (position pointer)
oCursor:= oText:createTextCursor()
oText:insertString(oCursor, ;
 "The first line in the created text document."+ CHR(10) + "FWH Rocks!", .F.)


// Save the File

cFileName:="c:\MyTest2.odf"
IF LEFT( cFilename, 1 ) != "/"
     cFileName:= "/" + cFileName
ENDIF
cURL:= StrTran( cFilename, "\", "/" )   // change backslashes to forward slashes.
cURL = "file://" + cURL

aProp:={}
AAdd(aProp,GetPropertyValue(oService, "Overwrite", .T. )  )
oDoc:storeToUrl( cURL, aProp ) 

//-----------------------------------------------------//

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



Do you also know the commands to to open an existing document and to save(as)?


Do you mean to Save as PDF or other formats ?. Or Open an existing document and save it with a different file name ?

Regards

Anser
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Open/Save document with OpenOffice Writer
Posted: Mon Mar 16, 2009 06:25 PM

Anser,

The open file is working very nice.
The save as doesn't work here. I open a .doc-file and wanted to save it to another .doc-filename.

Ofcource, I can also first make a copy of the file to a new file with another name and open it...

Do you know how to do a replace? I want to open a file and replace some strings with another string and than save it.

Thanks,
Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Open/Save document with OpenOffice Writer
Posted: Tue Mar 17, 2009 05:00 AM
Marc,

The save as doesn't work here. I open a .doc-file and wanted to save it to another .doc-filename.


The commands posted above is not for Save as but for Saving a Document. You may use the below given code to do a Save as
Code (fw): Select all Collapse
oService   := CreateObject( "com.sun.star.ServiceManager" )
oDesktop := oService:CreateInstance( "com.sun.star.frame.Desktop" )

// Opens a specific Document
aProp:={}
oDoc    := oDesktop:LoadComponentFromURL( "file:///C:/MyTest/Samples/Resume.odt", "_blank", 0, aProp )

// Insert sample Text
oText:=oDoc:getText() // Create text object

// Create a cursor object (position pointer)
oCursor:= oText:createTextCursor()
oText:insertString(oCursor, ;
 "The first line in the created text document."+ CHR(10) + "FWH Rocks!", .F.)

// Alternative method to execute a Save as
oDispatcher:= oService:CreateInstance( "com.sun.star.frame.DispatchHelper" )

aProp:={}
AAdd(aProp,GetPropertyValue(oService, "URL", "file:///C:/MyTest/ansertest.odt")  )  // File Name
AAdd(aProp,GetPropertyValue(oService, "FilterName", "writer8")  )
oDispatcher:executeDispatch(oDoc:GetCurrentController():GetFrame(), ".uno:SaveAs", "", 0, aProp)

//-----------------------------------------------------//

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


Do you know how to do a replace? I want to open a file and replace some strings with another string and than save it.


It will depend on your requirment. If you are you looking for a solution for search and replace, then here it is

Code (fw): Select all Collapse
aProp:={}
AAdd(aProp,GetPropertyValue(oService, "SearchItem.StyleFamily"  , 2       )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.CellType"     , 0       )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.RowDirection" , .T.     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.AllTables"    , .F.     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.Backward"     , .F.     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.Pattern"      , .F.     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.Content"      , .F.     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.AsianOptions" , .F.     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.AlgorithmType", 0       )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.SearchFlags"  , 65536   )  )
// Search String
AAdd(aProp,GetPropertyValue(oService, "SearchItem.SearchString" , "Antony")  )
// Replace String
AAdd(aProp,GetPropertyValue(oService, "SearchItem.ReplaceString", "Jerry" )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.Locale"       , 255     )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.ChangedChars" , 2       )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.DeletedChars" , 2       )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.InsertedChars", 2       )  )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.TransliterateFlags",1280 ) )
AAdd(aProp,GetPropertyValue(oService, "SearchItem.Command"      , 3       )  )
AAdd(aProp,GetPropertyValue(oService, "Quiet"                   , .T.     )  )

oDispatcher:executeDispatch(oDoc:GetCurrentController():GetFrame(), ".uno:ExecuteSearch", "", 0, aProp)


Regards

Anser
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Open/Save document with OpenOffice Writer
Posted: Tue Mar 17, 2009 07:11 PM

Thank you very much Anser,

I will try it.

Regards,
Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite

Continue the discussion