Macros to open file and export keeps giving runtime error

Hello,
i’m trying to achive file conversion task. I need to convert many old XLR ( Microsoft Works Spreadsheet) files into ODS format.
With help of ChatGPT i created this macro:

REM  *****  BASIC  *****

Sub ConvertToODS
    Dim sourceFolder As String
    Dim targetFolder As String
    Dim filePicker As Object
    Dim fileList As Object
    Dim file As Object
    Dim inputFilePath As String
    Dim outputFilePath As String
    Dim fileName As String
    Dim document As Object

    ' Specify source and target folders'
    sourceFolder = "file:///C:/Import-xlr/"
    targetFolder = "file:///C:/Export-xlr/"

    ' Create a file picker to get the list of files in the source folder'
    filePicker = createUnoService("com.sun.star.ucb.SimpleFileAccess")
    fileList = filePicker.getFolderContents(sourceFolder, False)

    ' Loop through each file in the folder'
    For Each file In fileList
        inputFilePath = file
        fileName = filePicker.getFileName(inputFilePath)

        ' Check if the file is a spreadsheet (looking for .xls or .xlsx files)'
        If InStr(fileName, ".xlr") > 0 Then
            
            ' Open the spreadsheet document'
            document = StarDesktop.loadComponentFromURL(inputFilePath, "_blank", 0, Array())

            ' Save it in the .ods format in the target folder'
            outputFilePath = targetFolder & Left(fileName, Len(fileName) - InStrRev(fileName, ".")) & ".ods"
            document.storeAsURL(outputFilePath, Array(MakePropertyValue("FilterName", "calc8")))

            ' Close the document after saving'
            document.close(True)
        End If
    Next file
End Sub

' Helper function to set properties'
Function MakePropertyValue(n As String, v As Variant) As Object
    Dim propValue As Object
    propValue = createUnoStruct("com.sun.star.beans.PropertyValue")
    propValue.Name = n
    propValue.Value = v
    MakePropertyValue = propValue
End Function

But when i ran it, i receive this error: BASIC runtime error. Object variable not set.
And process ends on the line “For Each file In fileList”. how can i resolve this?

thank you

more likely on this AI hallucination :

        fileName = filePicker.getFileName(inputFilePath)

for (human) reference : LibreOffice: XSimpleFileAccess Interface Reference

try from some Terminal:

soffice --convert-to ods C:\Import-xlr\*.xlr --outdir C:\Export-xlr\
1 Like

magic :slight_smile:
thank you!

so simple :))

you can see now, ChatGPT is dumb as shit, why do you use it?

ChatGPT is a tool, you still need to make proper requests to get right answers :slight_smile:

and a proper model :innocent:

The problem is not in ChatGPT, the problem is in users who believe (and made believe, by the massive advertising efforts) that it’s possible to replace own knowledge with tools. Every tool is only useful, when you can do it also without the tool - then you are always at the control.

Take a very simple example. In school, we are taught something about square roots. We roughly understand what to expect, when we take the roots, even if we don’t know a result of a random operation by heart. So if you use a tool like a calculator, and you don’t know that calculator’s key press sequence meaning, it may happen that you press: 7+4, expecting that the sum was performed prior to taking the root, so you will get square root of 11. But let’s say, that this specific tool (calculator) uses operator precedence rules, where square root has precedence over summation; then you will get a sum of 7 and square root of 4. You can spot the problem, because you of course know that square root of 11 can’t be a whole number (even if you don’t remember its approximate value of 3.316); but if you don’t have a slightest idea about square roots, you will get that result of 9 as the “needed” value of square root of the sum, not realizing that you used the tool incorrectly. And using that value in your further calculations, you would arrive at arbitrarily incorrect final answer of e.g. dosage of a medicine. (Note that even if Calculator gave you nor a round number for your arbitrary calculation, like e.g. 9.15, you could still spot the problem, knowing that square root of 9 is 3, and square root of 16 is 4 - so square root of 11 can’t be more than 9 - that’s called expertise: you roughly know what to expect, and can tell obvious bullshit right away; you use the tools to improve accuracy and performance, not to substitute ignorance).

I don’t believe in this popular way of use of ChatGPT.

1 Like

in this case I would expect the square root of 93 :innocent:

:slight_smile: That’s what happens when you change numbers several times while typing :slight_smile: Thanks

You do have a point. but look at it from a different perspective. We cannot know everything.
I agree that AI shouldn’t be used in place of education, but my job doesn’t deal with programming, so, ChatGPT helps a lot, in fields i’m not knowledgeable enough.

This is strictly true. But unfortunately, this truth is converted, during education times, into a false belief that we shouldn’t even try to know all of school education - making people not know some basic facts about the world we live in. Bug I digress.

… without any hope to find out, when the result of that collaboration would not give you an error message box, but provide with a false result.


By the way: why do you want to mass-convert? Please note, that keeping the documents in their original format, and only convert them one by one, when you start working with each - is usually better: the conversion is destructive; and a future LO version might be better in conversion - so if you keep File X in XL* format, until you need to work with that File X a year later - you would use LibreOffice V+2 to convert it, and the result might be better…

I’m migrating one small client to cloud. They have a lot of old documents in Microsoft Works (software from 90s :slight_smile: ) format (wps and xlr).

new MS Word opens wps just fine, but Excel would not work with xlr. LibreOffice is able to open them and save. I want to automate the process.
I have actually tried running soffice in command line before i posted here. But i must have missed something, as it didn’t work.

ah - so the end goal is to work with those files using Excel? Then why use the ODS out format? MS tools are much worse in handling of ODF, than LibreOffice in conversion into MS native formats (none is perfect, but the priorities of Microsoft are different).

maybe excel is a bit pity about the fileextension .xlr ?
Did you try just to rename your ….xlr-files to …xls and then open with excel ??

As your approach is also found elsewhere, it may be sufficient to tell Windows to register .xlr-extension to Excel for a double-click.
Second problem may be internal “spreadsheet filter” only showing files with extensions .xls and .xlsx

https://answers.microsoft.com/en-us/msoffice/forum/all/how-do-i-convert-xlr-files-to-xls-files/c8b801ee-4760-460f-a2c5-70a344868d4a

But from older times I remember MS-office needed some compatibility pack of additional filters to read files from Works. So this may be something to find first.

i’ve tried all that :slight_smile:
thats why i’m here, as Libre is the only one that opens it.
i even found Works 9, but everything would have to be done manually

I saw a native batch converter in Libre, but it only dealt with new formats.
My plan was to use macros to convert to ods, and than use native converter to make xlsx.
I haven’t tried it yet, but good chance your original solution will do it directly.

yes, it didn’t work with those particular files. it showed jibberish.

unfortunatly you didnt tell about that in your initial post!