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 25.2.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
This is Lupp's code slightly modified to add his result to the status bar, wait half a second and return the status bar to normal. You just need a shortcut to run the macro.
Change the 500 number to a different value if you want more time to look at the result.
Sub lineAndPosInText2()
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
cCtrl.select(oldSel)
lbl = ( "Line:" & LinesAbove + 1 & "; Position in line:" & charsLeft + 1 & Chr(10) & _
IIf(inTableCell<>"", "Table cell " & inTableCell, "") & IIf(inFrame, "Textframe!", ""))
sbar = pdoc.getCurrentController.StatusIndicator 'add to the status ar
sbar.start( lbl,0 )
Wait 500 'wait a bit so you can look at it CHANGE 500 TO PREFERRED TIME 500 = HALF A SECOND 1000 = 1 SECOND
sbar.end()
End Sub
Function tryCellName(pObj)
tryCellName = ""
On Local Error Goto fail
tryCellName = pObj.CellName
fail:
End Function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Sub lineAndPosInText2()
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
cCtrl.select(oldSel)
lbl = ( "Line:" & LinesAbove + 1 & ", Col:" & charsLeft + 1 & Chr(10) & _
IIf(inTableCell<>"", "Table cell " & inTableCell, "") & IIf(inFrame, "Textframe!", ""))
sbar = pdoc.getCurrentController.StatusIndicator
sbar.start( lbl,0 )
Wait 1000
sbar.end()
End Sub
Function tryCellName(pObj)
tryCellName = ""
On Local Error Goto fail
tryCellName = pObj.CellName
fail:
End Function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)