Ask Your Question

Writer: Convert text to columned table [closed]

asked 2018-02-16 20:46:33 +0200

paul1149 gravatar image

updated 2018-02-19 13:59:04 +0200

I filed a feature request over at 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:


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

It converts to:

word1 col2 col3

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( "" ) Then
    Dim iNumColumns As Integer 
    iNumColumns =  InputBox("Enter desired number of columns (blank = 0)")
    If iNumColumns <= 0 Then GoTo Exit1
End Sub
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by paul1149
close date 2018-02-19 15:48:05.589696


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

Paul451 gravatar imagePaul451 ( 2018-02-17 05:49:36 +0200 )edit

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

paul1149 gravatar imagepaul1149 ( 2018-02-17 06:07:06 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-02-19 13:24:50 +0200

librebel gravatar image

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( "" ) 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( "" )
        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

edit flag offensive delete link more


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.

paul1149 gravatar imagepaul1149 ( 2018-02-19 13:51:09 +0200 )edit

You're welcome @paul1149, Always glad if my solution is appreciated :)

librebel gravatar imagelibrebel ( 2018-02-19 14:14:03 +0200 )edit

Question Tools

1 follower


Asked: 2018-02-16 20:46:33 +0200

Seen: 68 times

Last updated: Feb 19 '18