Open/Save document with OpenOffice Writer

Open/Save document with OpenOffice Writer

Postby Marc Vanzegbroeck » 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: Select all  Expand view
     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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Open/Save document with OpenOffice Writer

Postby anserkk » Mon Mar 16, 2009 11:55 am

Marc,

Try this for a start

Code: Select all  Expand view
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
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Open/Save document with OpenOffice Writer

Postby Marc Vanzegbroeck » Mon Mar 16, 2009 1: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Open/Save document with OpenOffice Writer

Postby anserkk » Mon Mar 16, 2009 4:39 pm

Dear Marc,

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

Code: Select all  Expand view
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
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Open/Save document with OpenOffice Writer

Postby Marc Vanzegbroeck » Mon Mar 16, 2009 6: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Open/Save document with OpenOffice Writer

Postby anserkk » Tue Mar 17, 2009 5: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: Select all  Expand view

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: Select all  Expand view
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
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Open/Save document with OpenOffice Writer

Postby Marc Vanzegbroeck » Tue Mar 17, 2009 7: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 100 guests