When a text table is at the very top of a page, the problem begins with
which places the cursor in the first cell of the text table, but
oRangePageEnd is outside the text table, thus the error.
Here is a version that tests if the cursor is in a text table then it inserts a blank paragraph before the text table.
Code: Select all
Sub ko()
Dim VisibleCursor as object
Dim oRangePageEnd
VisibleCursor = ThisComponent.CurrentController.ViewCursor
VisibleCursor.jumpToPage(1)
VisibleCursor.jumpToStartOfPage
REM test if VisibleCursor is inside a text table
if isempty(VisibleCursor.TextTable) = FALSE then
InsertParBeforeTable() 'VisibleCursor is inside text table so insert a blank paragraph before table
end if
VisibleCursor.jumpToEndOfPage
oRangePageEnd = VisibleCursor.end
VisibleCursor.jumpToStartOfPage
VisibleCursor.gotoRange(oRangePageEnd, true)
End Sub
REM "Listing 7.5: Insert a new paragraph before a text table" from "Useful Macro Information" by Andrew Pitonyak
Sub InsertParBeforeTable
Dim oTable
Dim oText
Dim oPar
oTable = ThisComponent.getTextTables().getByIndex(0)
oText = ThisComponent.getText()
oCurs = oText.createTextCursor()
oPar = ThisComponent.createInstance("com.sun.star.text.Paragraph")
oText.insertTextContentBefore ( oPar, oTable )
End Sub
There is a potential problem. If the page has no space at the bottom, then inserting a blank paragraph will cause the last line on the page to be pushed onto a new page, and only that line will be selected.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11