Ask Your Question

Writer Basic - retrieve TextTable from TextTableCursor (originally "4.1")

asked 2013-11-05 16:00:22 +0200

Yimin Rong gravatar image

updated 2020-11-12 17:08:36 +0200

Lupp gravatar image

I would like to write a macro that works on the selected table. When I select a table, the object that ThisComponent.CurrentSelection returns is of type SwXTextTableCursor (will refer to it generically as TextTableCursor). According to DBG_methods it provides methods to traverse through the selected cells and merge or split the cells, but it doesn't seem to provide a way to access the actual table itself. Conversely, ThisComponent.TextTables returns the tables, but as far as I can tell, no way to determine if any or all of a table is selected.

Is there any way to retrieve the TextTable(s) from TextTableCursor?

Thanks for reading.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2020-11-12 15:37:21 +0200

Lupp gravatar image

updated 2020-11-12 15:39:32 +0200

(Question reopened.)

TextTableCursor objects are poorly equipped, and to work with TextTable objects based on the API often seems to be a game of chance.
I had the given question in mind more than once, and nowhere found a ready-made solution. Recently I again stumbled into that donkeywork, and actually I managed to learn a bit more. The progress allowed me to also find a solution for this question.
Accepting a "dirty but not at all quick" approach, I can now retrieve the TextTable from a TextTableCursor! Wow!
Funny code:

Function getTextTableFromSelection(pDoc As Object, Optional pSel As Object)
Dim tT As Object
getTextTableFromSelection = tT
On Local Error Goto fail
cCtrl = pDoc.CurrentController
oldSel = pDoc.CurrentSelection
If IsMissing(pSel) Then pSel = oldSel
If pSel.supportsService("") Then
  wasCur = True
  splRgN = Split(pSel.RangeName, ":")
  wasSingle = (Ubound(splRgN)=0)
  tlCellN = splRgN(0)
  pSel.gotoCellByName(tlcellN, False)
  tRg = pDoc.CurrentSelection(0)
  tRg = pSel(0)
End If
textTable = tRg.TextTable
If NOT IsObject(textTable) Then textTable = tT
getTextTableFromSelection = textTable
If wasCur AND wasSingle Then
  fr = cCtrl.Frame
  dh = CreateUnoService("")
  dh.executeDispatch(fr, ".uno:EntireCell", "", 0, Array())
End If
End Function

Sub tryIt()
doc = ThisComponent
sel = doc.CurrentSelection
h = getTextTableFromSelection(doc, sel)
End Sub

With a lot of dirty tricks I might also be able to get the document from the TextTableCursor (or any selection in the doc), but I didn't do that. To know the document is necessary to be able to use the CurrentController, which is needed for ...
See yourself!

edit flag offensive delete link more

answered 2015-07-21 08:40:20 +0200

doug gravatar image

Here are the equivalent objects.

This object is retrieved without reference to the cursor:

SwXTextTable = ThisComponent.TextTables.getByIndex(0)

This object is derived from the cursor:

cntrllr = ThisComponent.CurrentController
cursor = cntrllr.ViewCursor
SwXTextTable = cursor.TextTable

You can test this with the following operation, which will print out the contents of the top left cell of whichever text table you have invoked:

oCell = SwXTextTable.getCellByPosition(0,0)
MsgBox oCell.String
edit flag offensive delete link more

answered 2015-07-23 14:34:38 +0200

pierre-yves samyn gravatar image


I do not know if this is helpful but could complete doug's response on "determine if a table is selected":

dim oCursor as object

oCursor = thiscomponent.currentcontroller.viewcursor

if isempty(oCursor.TextTable) then  
    msgbox "Current selection: no table"
    msgbox "Current selection:  " &
end if


edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-11-05 16:00:22 +0200

Seen: 725 times

Last updated: Nov 12 '20