From The Document Foundation Wiki
< Macros‎ | Writer
Jump to navigation Jump to search
This page is a translated version of the page Macros/Writer/008 and the translation is 9% complete.
Other languages:
English • ‎español • ‎français • ‎italiano


For LibreOffice Writer, we wish to write a macro which emulates the functionality of Table ▸ Convert ▸ Table to Text....

We will use the dispatcher to engage the command .uno:ConvertTableToText (this part of the code is can be recorded with if Tools ▸ Options ▸ Advanced ▸ Enable macro recording (may be limited) 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[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.


In LibreOffice Basic:

Option Explicit

Sub Main()
End Sub

Sub TableToText(TableName As String)
    '''Convert a table to text'''

    Dim oDoc As Object ' (ThisComponent)
    Dim oTable As Object '
    Dim oTableCursor As Object '
    Dim document As Object ' (ThisComponent.CurrentController.Frame)
    Dim dispatcher As Object '
    Dim args(0) As New

    oDoc = ThisComponent
    oTable = oDoc.TextTables.getByName(TableName)
    oTableCursor = oTable.createCursorByCellName("A1")

    document = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("")

    args(0).Name = "Delimiter"
    args(0).Value = CHR$(9) ' ASCII char #9 = TAB

    dispatcher.executeDispatch(document, ".uno:ConvertTableToText", "", 0, args())

End Sub ' TableToText

ODT file to test macro