Problem with named range creation using LibreOffice Basic [SOLVED]

I am developing an application in Calc that uses the following code to name a range.

Dim Rname As String
Dim s As String
Dim Doc As Object
Dim Ranges As Object
Dim Sheet As Object
Ranges = Doc.NamedRanges
Sheet = Doc.Sheets.getByName("Sheet1")
Rname = "TestRange"
s = "$Sheet1.$C$11"
Ranges.addNewByName(Rname, s, Sheet.getCellByPosition(2, 10).CellAddress, 0)

The problem is that the code works fine in one computer running Ubuntu 22.10 and LibreOffice 7.4.4.2 but fails in another (the one that is supposed to run the application) with Ubuntu 22.04 LTS and LibreOffice 7.3.7.2.
On the second computer I get a runtime exception “Type: com.sun.star.uno.RuntimeException Message: ./sc/source/ui/unoobj/nameuno.cxx:518.”

This type of failure worries me as it raises questions to the portability of the application to other computers.
Any help will be greatly appreciated. Thank you

What makes you sure that there is always a sheet named “Sheet1”? Things like this should be handled by means of templates and styles rather than stupid code.

I am sure that “Sheet1” exists because I created it in this test scenario. In the original application I have a test that checks if it exists or otherwise creates it. Any way, in both cases (test and real application) the Doc, Sheet, Ranges and everything else exists and works. The only difference is the error I describe in one of the computers. I plan to test the same code in another Linux computer and one with Windows. Thank you for your comment

this error exactly occurs if you run the same code twice ( of course »TestRange« exists already, the same name cannot be used again )
Here the proof-screenshot:

1 Like

Thank you. This solved the issue. I had not realized that the sub run once in the application and had created the names. I thought that it had failed immediately. On the test platform I was always deleting the name before testing.