What code will sort a Calc sheet in vb6?

I’m translating a series of VB6 packages to work with LO Calc instead of MS Excel and am doing fine except for sorting. The following code raises no errors but doesn’t work and I can’t see why. It works fine as a macro.

Sub SortRangeF()

Dim loSheet        ' loCalc sheet containing data to sort.
Dim loCellRange    ' Data range to sort.
Dim loSortFields(0)
Dim loSortDesc(1)

loSheet = loCalc.sheets.getByName("Sheet1")

loCellRange = loSheet.getCellRangeByName("B1:D50")

loCalc.getCurrentController.Select loCellRange  'Only to highlight the range.

loSortFields(0) = MakeSortField(2, True)        'Sort on column C ascending.
loSortDesc(0) = MakePropertyValue("SortFields", loSortFields())
loSortDesc(1) = MakePropertyValue("ByRows", True)

loCellRange.sort (loSortDesc())
'CallByName loCellRange, "Sort", VbMethod, loSortDesc()

End Sub

MakeSortField and MakePropertyValue are functions which work in other contexts. loCalc is the workbook object. I’ve also tried using CallByName with the same result: no errors, no sort.

OS Windows7 Prof. LO

I not see your instance for variable for loSortDesc, I mean, your declare, but, you need initialize like:

loCellRange = loSheet.getCellRangeByName("B1:D50")

loSortDesc = loCellRange.createSortDescriptor()



Thanks for you comment on my question.

I had thought that the function which creates loSortFields(0) would have initialised it.

'Routine to create a sequence of com.sun.star.util.SortField
'Function name: MakeSortField(sFld, sValue)
'Parameters: sFld; the Field name as a string,
’ sValue; the Sort direction, True = ascending, False = descending.

Public Function MakeSortField(sFld, sValue) As Object

Dim loStruct As Object
Set loStruct = loSM.Bridge_GetStruct("com.sun.star.util.SortField")
loStruct.Field = sFld
loStruct.SortAscending = sValue
Set MakeSortField = loStruct

End Function

I take it from your comment that I should be creating a ‘SortDescriptor’ rather than a SortField’. I shall try this approach.

Thanks again.