Using PB 2019, I'm trying to use OpenOffice through OLE to open an Excel doc and save it as CSV. (Yes, I can do that Excel, but this is for a situation where OpenOffice is available but Excel isn't.)
I'm running into two problems: the SaveAsURL (or SaveToURL) always fails with an OpenOffice IOError, and the OpenOffice application never closes (as seen on Task Manager).
Here's my code (in a function taking asFilename as its argument):
iole_app = CREATE OLEObject
OLEobject desktop, prop, props[], document
string fixedFilename, saveFilename
int pos
if iole_app.ConnectToNewObject("com.sun.star.ServiceManager") <> 0 THEN
MessageBox('Test', 'Unable to start OpenOffice.',Exclamation!)
DESTROY iole_app
return FALSE
end if
desktop = iole_app.createInstance("com.sun.star.frame.Desktop")
prop = iole_app.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
prop.Name = "Hidden"
prop.Value = TRUE // don't want to show UI
props[1] = prop
fixedFilename = "file:///" + asFilename
gf_replace_all(fixedFilename, "\", "/") // replaces the 2nd arg with the 3rd throughout the 1st arg
document = desktop.loadComponentFromURL(fixedFilename, "_blank", 0, props)
pos = LastPos(Lower(fixedFilename), ".xls")
if pos = 0 then
MessageBox("Test", "Cannot find .xls in filename")
DESTROY iole_app
return FALSE
end if
saveFilename = Mid(fixedFilename, 1, pos) + "csv"
props[1].Name = "FilterName"
props[1].Value = "scalc: Text - txt - csv (StarCalc)"
props[2] = iole_app.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
props[2].Name = "Overwrite"
props[2].Value = TRUE
try
document.storeToURL(saveFilename, props)
catch (OLERuntimeError ex)
MessageBox("Test", "Error " + ex.description + " saving to " + saveFilename + "~n~n: " + ex.text)
end try
document.close(TRUE)
DESTROY document
SetNull(document)
desktop.dispose()
DESTROY desktop
SetNull(desktop)
DESTROY iole_app
SetNull(iole_app)
return TRUE
I know I am doing overkill on the DESTROYs and also SetNull, but I'm trying everything. If I change the property "Hidden" used in the properties sent to loadComponentFromURL() to FALSE (or omit it, since FALSE is the default) the application does close after the code finishes.
But document.storeToURL (or the alternative storeAsURL) always throws a com.sun.star.task.ErrorCodeIOException, with no further helpful details. I have checked that the file URL it is supplying is OK, and I know that the directory the file is in is writeable.
Any bright ideas? Thanks.
If your file doesn't have accented characters, just go back to 0 as the 3rd element of the FilterOptions. Otherwise, you will have to fix the file somehow to have that BOM. (That turned out to be really difficult in PowerBuilder - I tried several things, then gave up and used a 3rd-party control I had to do that fix.)