by Lupp » Mon Nov 23, 2020 1:31 am
Q&D:
- Code: Select all Expand viewCollapse view
Sub lineAndPosInText(Optional pDoc)
REM If the text object of the current selection contains interceptive tables,
REM the lines of the leftmost column should be counted as if they are lines
REM of ordinary paragraphs - independent of the heights of the cells.
REM TextTable cause problems again and again. Not sure if my stetement is reloable.
If isMissing(pDoc) Then pDoc = ThisComponent
oldSel = pDoc.CurrentSelection
currPos = oldSel(0).End
cCtrl = pDoc.CurrentController
vc = cCtrl.ViewCursor
currText = vc.Text
inTableCell = tryCellName(currText)
inTable = (inTableCell<>"")
inFrame = currText.supportsService("com.sun.star.text.BaseFrame")
vc.gotoRange(currPos, False)
vc.goToStartOfLine(False)
REM Strange! The ViewCursor.String is empty after vc.goToStartOfLine(True) if in a TextTable cell.
REM Therefore:
tcPos = currText.createTextCursorByRange(currPos)
tcPos.gotoRange(vc.Start, True)
charsLeft = Len(tcPos.String)
trgLineStart = vc.Start
If EqualUnoObjects(currText, pDoc.Text) Then
REM For the BodyText line counting is done per page.
vc.jumpToStartOfPage()
Else
REM Otherwise for the current text not regarding pages.
cCtrl.select(currText.Start)
End If
LinesAbove = 0
REM TextTable are a mess!!
REM The following may NOT work if nested TextTable objects exist.
REM Will not work araound! Someone else?
While currText.compareRegionStarts(vc.Start, trgLineStart)>0
LinesAbove = LinesAbove + 1
vc.goDown(1, False)
Wend
MsgBox("Line:" & LinesAbove + 1 & "; Position in line:" & charsLeft + 1 & Chr(10) & _
IIf(inTableCell<>"", "Table cell " & inTableCell, "") & IIf(inFrame, "Textframe!", ""))
cCtrl.select(oldSel)
End Sub
Function tryCellName(pObj)
tryCellName = ""
On Local Error Goto fail
tryCellName = pObj.CellName
fail:
End Function
We may
feel sure there are only simple cases without nested tables or frames or whatever, but we cannot
be sure.
That "rare complications" to be expected make reliable solutions rare.
On Windows 10: LibreOffice 7.0 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München