Page 1 of 1

[Solved] Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 2:27 pm
by esperantisto
I’m trying to make a Calc macro that exports a (currently) spreadsheet to a tab-delimited file in UTF-8. Ideally, it should save under the same file name replacing .odt/.xls/.xslx with .txt, but for testing purposes I just hard-code the file name so far. Thus, the code is:

Code: Select all

Sub ExportToGlossary

Dim oDoc as Object
Dim glossaryFileProperties(3) as new com.sun.star.beans.PropertyValue
Dim sURL as String

oDoc = ThisComponent

If oDoc.getLocation() = "" Then Exit Sub

sURL = "file:///E:/export_to_glossary.txt"

glossaryFileProperties(0).Name = "FilterName"  ' setting properties of exported file such as tab as field delimiter, nothing as text delimiter, UTF-8 as encoding
glossaryFileProperties(0).Value = "scalc: Text - txt - csv (StarCalc)" 
glossaryFileProperties(1).Name = "FilterOptions" 
glossaryFileProperties(1).Value = "9,0,76,1,,0,false,true,false"
glossaryFileProperties(2).Name = "Overwrite" 
glossaryFileProperties(2).Value = True

oDoc.storeAsURL(sURL, glossaryFileProperties())
End Sub
When I run the macro from Apache OpenOffice 4.2.0 (420m1 Build:9800), I get the following error message:

Code: Select all

BASIC runtime error.
An exception occurred
Type: com.sun.star.task.ErrorCodeIOException
Message:.


(Damn those programmers that don’t bother about producing clear error messages!)

LibreOffice 5.0.0.4 is somewhat more eloquent:

Code: Select all

BASIC runtime error.
An exception occurred
Type: com.sun.star.task.ErrorCodeIOException
Message: SfxBaseModel::impl_store <fiIe:///E:/expоrt_to_glоssary.txt> failed: 0x81a.
However, I still can’t understand what’s wrong. Here’s an example from StarBasic Programming Guide:

Code: Select all

Dim mFileProperties(0) As New com.sun.star.beans.PropertyValue
Dim sUrl As String
sUrl = "file:///complete/path/To/New/document"
mFileProperties(0).Name = "Overwrite"
mFileProperties(0).Value = FALSE
oDocument.storeAsURL(sUrl, mFileProperties())
I can’t see any substantial difference as compared to my code.

Can anyone give me a pointer what should I look at?

P. S. I have full read/write permissions on the partition concerned.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 5:05 pm
by Zizi64
Tips:

1.: Device 'E:' is not available
2.: Device 'E:' is read only

If you want EXPORT some data then you need use the 'storeToURL' command instead of the 'storeAsURL'.
The 'storeAsURL' equivalent to the Save As function: the source file being a .txt file: the original .ods file will be closed, and you can edit the newly created .txt file.
The 'storeToURL' will create a .txt standalone file, but the original .ods file stay opened.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 7:41 pm
by esperantisto
Thanks, but your tips don’t help:
a) the partition e: is available and writeable (it’s my working partition under Windows);
b) storeToURL doesn’t work either.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 7:43 pm
by Villeroy
You set Overwrite=False, so I guess you are the programmer supposed to handle the error which is raised when the file exists.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 7:45 pm
by esperantisto
No, it’s true (see the first listing), false is in the example from the programming guide. Besides, it won’t work even if I manually delete the text file.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 8:02 pm
by FJCC
Try setting the filter to
Text-txt-csv (StarCalc)
That is the name I see in the Developer's guide and I ran into problems with the one you are using many years ago.

Your filter options (9,0,76,1,,0,false,true,false) also look strange to me. Do you need to use Character set = 76? Try using 9,0,0,1,1.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Thu Sep 10, 2015 10:29 pm
by B Marcelly
Yes, the correct Filter name is :
Text - txt - csv (StarCalc)
Copy exactly this name, spaces and letter case are important.

Re: Error with storeAsURL: can’t figure out the reason

Posted: Fri Sep 11, 2015 8:34 am
by esperantisto
FJCC and B Marcelly, thanks a lot for the pointer! It was exactly the filter name that was bad. It’s strange because "scalc: Text - txt - csv (StarCalc)" works fine in a couple of macros that I use for loading certain CSV files.

FJCC, I got those filter options by manually saving a file to CSV with macro recording on. I’ll try your suggestion, thanks!

Zizi64, thank you for the pointer about storeToURL()! Now, when I got the macro working, I see that storeToURL() is what I need rather than storeAsURL().

The problem is solved. The next task is changing the file name rather than hardcoding. Thus, I’ll be back with new questions :-)