How to sort a texttable in writer in Basic

Hi all. I’m trying to sort a table in a document via a macro. I can find the table of interest and also select the whole table ready for sorting, but a command I found that is supposed to work isn’t doing anything - sortTextTable. Using the menu to sort works fine.

sortAscend = True 'asc sorting
headerPresent = True 'table contains a header row
sortingCol = 1 ''column number to sort on

tableList = ThisComponent.getTextTables() 

For indexNum = 0 To tableList.getCount() - 1

    tableItem = tableList(indexNum) 'Get the individual table object
    tableName = tableItem.getName() 'get the table name

    If tableName = "Table1" Then
        tableViewCursor = ThisComponent.CurrentController.getViewCursor()
        ThisComponent.CurrentController.select(tableItem)
        tableViewCursor.gotoEnd(True) 'Move to the end of the current cell.
        tableViewCursor.gotoEnd(True) 'Move to the end of the table.
        tableItem.sortTextTable(tableName, sortAscend, headerPresent, sortingCol)
    End If

Next

Any help is appreciated

Version: 25.2.3.2, Windows 10

You’ve been fooled by AI.
Method sortTextTable is hallucinated.
Better use this site’s search function.

1 Like

(post deleted by author)

Indeed?

Maybe I just don’t see it. Please show me where in this form I can specify “Table contains headers”.
image

Or find this item in the help for me.
As far as I know, text tables have been sorted completely so far. In your case, the code could be like this:

Sub SortTable1
Dim oTextTables As Variant
Dim oTextTable As Variant
Dim oSortDescriptor As Variant
	oTextTables = ThisComponent.getTextTables()
	For Each oTextTable In oTextTables
		If oTextTable.getName() = "Table1" Then
			oSortDescriptor = oTextTable.createSortDescriptor()
' Since you are sorting by the first column in ascending order, and these are '
' the default values, no additional actions are required '
			oTextTable.Sort(oSortDescriptor)
' If you had chosen some other column or some other sort order, '
' then you would indeed have to describe the SortFields array, '
' find where to insert it in oSortDescriptor - a lot of code, little sense. '
			Exit For
		EndIf
	Next oTextTable
End Sub
1 Like

@JohnSUN I found the code from here, obviously it is not working. I presume the headerPresent option was to tell the function if there were headings or not. Given that this was last updated in March 2023, things may well have changed since then.

And yes, selecting the whole table, including headings, works fine using the menu option. I guess the top row gets ignored when sorting.

Thank you for the code snippet, I’ll give it a go.

2 Likes

Hi @Villeroy , I don’t think this was AI, the code came from here

1 Like

Got it. Where are these lines in your code?

If bHeader Then
   nStartRow = 1
Else
   nStartRow = 0
End if

Update: By the way, if you copy the full code from that page, sortWriterTableAscendingExclHeader will work

@JohnSUN , dammit you’re right. I didn’t take notice of the rest of the code, stupid mistake.

Yours looks a lot simpler. Thanks again

Summary of the start message: :slight_smile:

Sub SortTextTable()
  Dim oTable As Object
  oTable= ThisComponent.TextTables.getByName("Table1")
  oTable.Sort oTable.CreateSortDescriptor()
End Sub