Writer: Convert text to columned table

I filed a feature request over at https://bugs.documentfoundation.org/show_bug.cgi?id=99035. What I’m trying to do is convert Selected text to a table, whose number of columns will vary, depending on the data. The problem is that in the scanned documents I work on, each data entry is on its own line:

word1 
word2
word3
word4
word5
word6

Someone mentioned the trick of adding tabs to the first line, and indeed, this creates a table with a corresponding number of columns, which is pretty neat. But the data is not filled into those extra columns, they remain empty. The data continues to be populated, each line into a new row. IOW, if the original text, with added tabs, is:

word1 tab tab
word2
word3
word4
word5
word6

It converts to:

word1 col2 col3
word2
word3
word4
word5
word6

Whereas I would like it to be:

word1  word2 word3
word4 word5 word6

I suppose the best way to get at this is a macro 1) converting linefeeds to delimiters, 2) within a selection, 3) leaving every Nth feed untouched(specified at the running of the macro), 4) applied before table conversion.

Has anyone dealt with this or have an idea? Thanks.

Edit:
Please see @librebel’s excellent solution below. It does exactly what I was asking for. I only added an Input box to spec number of columns on the fly, and a premature exit if that number is 0:

If oDocument.supportsService( "com.sun.star.text.TextDocument" ) Then
    Dim iNumColumns As Integer 
    iNumColumns =  InputBox("Enter desired number of columns (blank = 0)")
    If iNumColumns <= 0 Then GoTo Exit1
...
Exit1:
End Sub

What is the format of the original file? (Or what is it being generated from?)

The original is scanned plain text, which I then paste into LO unformatted and save as .odt.

Hello @paul1149,

The following method does what you want, but without considering text format.

Sub Writer_SelectionToColumns( ByRef iNumColumns As Integer, Optional sSeparator )
REM Convert selected Text into a specified number of Table Columns.
REM <iNumColumns> : The number of columns for the Table to be created.
REM <sSeparator>  : String pattern separating the Text portions to be inserted into the Table Cells.
REM NB. This method does not preserve the existing text format of the selection.
REM TODO: Format text; AutoFormat Table.
	Dim oDocument As Object : oDocument = ThisComponent
	If oDocument.supportsService( "com.sun.star.text.TextDocument" ) Then
	
		If iNumColumns <= 0 Then iNumColumns  = 1
		If IsMissing( sSeparator ) Or sSeparator  = "" Then sSeparator = chr(10)
		
		Dim oSelection As Object : oSelection = oDocument.CurrentController.Selection.getByIndex( 0 )
		Dim aParts() As String   : aParts     = Split( oSelection.getString(), sSeparator )
		Dim iNumRows As Integer  : iNumRows   = ( 1 + uBound( aParts ) ) \ iNumColumns
		If ( 1 + uBound( aParts ) ) Mod iNumColumns > 0 Then iNumRows = iNumRows + 1	REM Ceiling()...
		If iNumRows = 0 Then Exit Sub
		
		Dim oTable As Object     : oTable     = oDocument.createInstance( "com.sun.star.text.TextTable" )
		oTable.initialize( iNumRows, iNumColumns )
		
		oDocument.Text.insertTextContent( oSelection, oTable, True )	REM Replace selection with Table.
		
		Dim i As Integer
		For i = 0 To uBound( aParts )
			oTable.getCellByPosition( i Mod iNumColumns, i \ iNumColumns ).setString( aParts( i ) )
		Next i

	End If
End Sub

Hope you can use it,

With Regards, lib

Absolutely awesome, librebel. Does exactly what I was looking for. The formatting isn’t important, as the documents I work with are unformatted until I go over them. I added an Input box so I could spec number of columns on the fly, and if = 0 the macro would exit without doing anything. I will append it to my question. Thanks so much for taking the time to come up with this solution! Blessings.

You’re welcome @paul1149,
Always glad if my solution is appreciated :slight_smile: