Convert a table to text
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
Description
For LibreOffice Writer, we wish to write a macro which emulates the functionality of
▸ ▸ .We will use the dispatcher to engage the command .uno:ConvertTableToText (this part of the code is can be recorded with if
▸ ▸ ▸ is enabled).The macro starts by positioning the cursor in the table with the Select method of CurrentController and passing it the cursor created in the table.
For the below example, a table with the name "Table2" is converted.
Accessing of the table will be done through the method getByName from the service TextTables. The method com.sun.star.text.createCursorByCellName[1] allows for creation of a cursor in cell A1. This cursor is then used for selectioning in the table and can hence do the converting.
Code
In LibreOffice Basic:
Option Explicit
Sub Main()
TableToText("Table2")
End Sub
Sub TableToText(TableName As String)
'''Convert a table to text'''
Dim oDoc As Object ' com.sun.star.uno.XInterface (ThisComponent)
Dim oTable As Object ' com.sun.star.text.XTextTable
Dim oTableCursor As Object ' com.sun.star.text.XTextTableCursor
Dim document As Object ' com.sun.star.frame.XFrame (ThisComponent.CurrentController.Frame)
Dim dispatcher As Object ' com.sun.star.frame.DispatchHelper
Dim args(0) As New com.sun.star.beans.PropertyValue
oDoc = ThisComponent
oTable = oDoc.TextTables.getByName(TableName)
oTableCursor = oTable.createCursorByCellName("A1")
oDoc.CurrentController.select(oTableCursor)
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
args(0).Name = "Delimiter"
args(0).Value = CHR$(9) ' ASCII char #9 = TAB
dispatcher.executeDispatch(document, ".uno:ConvertTableToText", "", 0, args())
End Sub ' TableToText