[Solved]LoadComponentFromURL ignores FileProperties Argument

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
JulianR
Posts: 26
Joined: Mon Mar 12, 2018 9:41 am

[Solved]LoadComponentFromURL ignores FileProperties Argument

Post by JulianR »

Hello.

I am experimenting with opening calc documents, and I have run in to a strange problem. The code below is supposed to open a file in read only mode, but the file is opened always in read/write mode, and if the file is open in another frame or on another machine, the LoadComponent... returns an exception with an error message "com.sun.star.lang.IllegalArgumentException Message: URL seems to be an unsupported one.."

Code: Select all

Sub Test
Dim oDoc, oSheet, oCell As object
Dim fileName As String
Dim FileProp(0) As New com.sun.star.beans.PropertyValue

FileProp(0).Name = "ReadOnly"
FileProp(0).Value = "true" 'I have tried "True" as well as "1"

fileName = ConvertToURL("C:\Users\user\Documents\TEMP\Test.ods")
oDoc = StarDesktop.loadComponentFromURL(fileName, "_blank", 0, FileProp()) 'I have also tried "FileProp" without brackets
if NOT IsEmpty(oDoc) Then
oSheet = oDoc.Sheets.getByName("test")
oCell = oSheet.getCellByPosition(2,4)
msgBox oCell.String
else
msgbox "Could not open file"
end if

End Sub
My goal is to open the file in read-only mode (for two reasons, one, the file contains important data, and two, the files are often open for edition on other machines), extract some data and then close the file, preferably without opening the window, but "Hidden" property value is also ignored. How should I amend my code?
I can always use XSimpleFileAccess to copy the file to temporary location and delete it after use, but that is not an elegant solution in my mind.
Last edited by JulianR on Mon May 14, 2018 2:08 pm, edited 1 time in total.
Apache OpenOffice 4.1.1 / LibreOffice 5.3 / LibreOffice 6.0 / LibreOffice 6.2 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: LoadComponentFromURL ignores FileProperties Argument

Post by Villeroy »

"true" is a text, "1" is a text.
True is a boolean, 1 is a number which evaluates as True when used instead as a boolean.
Leave out the quotes and both values should work.

Read the first chapters of some programming book on any flavour of Basic or better learn a full featured modern language. This office can process Python macros instead of Basic.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JulianR
Posts: 26
Joined: Mon Mar 12, 2018 9:41 am

Re: LoadComponentFromURL ignores FileProperties Argument

Post by JulianR »

Thank you, that solved the problem. I simply don't get this variant datatype, which can hold anything, in C/C++/JAVA I have to be explicit about my datatype. Here anything goes.

And I think that Python is just as bad as Basic, Java perhaps would be the way to go, but it looks like it would be just a little less of a hassle to set-up than C++, so Star Basic is the way to go.
Apache OpenOffice 4.1.1 / LibreOffice 5.3 / LibreOffice 6.0 / LibreOffice 6.2 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Post by Villeroy »

In popular script languages anything quoted is a string.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JulianR
Posts: 26
Joined: Mon Mar 12, 2018 9:41 am

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Post by JulianR »

Yes, it is. Somehow I forgot, that I can simply use any datatype value in basic variable, so I thought I had to provide value as a string and that the software would do the conversion at runtime. My bad.
Apache OpenOffice 4.1.1 / LibreOffice 5.3 / LibreOffice 6.0 / LibreOffice 6.2 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Post by Zizi64 »

My goal is to open the file in read-only mode (for two reasons, one, the file contains important data, and two, the files are often open for edition on other machines), extract some data and then close the file, preferably without opening the window, but "Hidden" property value is also ignored. How should I amend my code?
1.: If you can handle the task with one macro routine from the opening to the closeing, then the users can not modify anything in the file.

2.: You can open a file in "hidden" state:

Code: Select all

 Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
	FileProperties(0).Name = "Hidden"
	FileProperties(0).Value = true
3.: You can use Template files instead of the Document fileformats. In this case the file will be open as "Untitled 1" document. It have not any URL. It will prevent the accidental saving of the file (you will not rewrite the original template)
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Post by Villeroy »

My goal is to open the file in read-only mode (for two reasons, one, the file contains important data, and two, the files are often open for edition on other machines), extract some data and then close the file, preferably without opening the window, but "Hidden" property value is also ignored. How should I amend my code?
This is what the database component is made for. It can be used as an invisible data storage with the capability to dump subsets of tables in any order of rows and columns into office documents. Templates, styles, data sources work very reliably without a single line of code.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply