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

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.

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