[Solved]LoadComponentFromURL ignores FileProperties Argument

Creating a macro - Writing a Script - Using the API

[Solved]LoadComponentFromURL ignores FileProperties Argument

Postby JulianR » Mon May 14, 2018 1:48 pm

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   Expand viewCollapse view
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 on Windows 7
JulianR
 
Posts: 5
Joined: Mon Mar 12, 2018 9:41 am

Re: LoadComponentFromURL ignores FileProperties Argument

Postby Villeroy » Mon May 14, 2018 1:53 pm

"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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 25763
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: LoadComponentFromURL ignores FileProperties Argument

Postby JulianR » Mon May 14, 2018 2:07 pm

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 on Windows 7
JulianR
 
Posts: 5
Joined: Mon Mar 12, 2018 9:41 am

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Postby Villeroy » Mon May 14, 2018 2:15 pm

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 25763
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Postby JulianR » Mon May 14, 2018 2:24 pm

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 on Windows 7
JulianR
 
Posts: 5
Joined: Mon Mar 12, 2018 9:41 am

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Postby Zizi64 » Mon May 14, 2018 5:15 pm

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   Expand viewCollapse view
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; LO4.4.7, LO6.1.1 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.2 and AOO4.1.5
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
Zizi64
Volunteer
 
Posts: 7312
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved]LoadComponentFromURL ignores FileProperties Argu

Postby Villeroy » Tue May 15, 2018 1:19 pm

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 25763
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests