Previous macro deleted the all 1st lines from 2nd page without any control whether the 1st line is only dashed line or not. And because it deletes whole line including the Enter at the end of line, it causes the shifting up of all text after deleted line. So it seems sometimes the dashed line from next page is shifted to the end of previous page (that was deleted 1st line). I see only one possibility
let the empty space instead of dashed line
Macro with easy control the 1st line is only dashed line; delete last page if empty; and execute the deleting lines whether the visible cursor is in bad position (in table or isn’t “focused” in document):
Sub empty1line 'empty all 1st lines from 2nd page to last page
on local error goto final
dim oDoc as object, oVCur as object, iPage&, i&, s$, undoMgr as object, oPos as object
const cUndo="delete 1st lines"
oDoc=ThisComponent
oVCur=oDoc.CurrentController.ViewCursor 'visible cursor
oPos=oVCur.start 'remember the position of visible cursor
iPage=oDoc.CurrentController.PageCount
if iPage<2 then exit sub 'only one page so exit
oVCur.jumpToFirstPage()
oDoc.lockControllers
undoMgr=oDoc.UndoManager 'undo manager
undoMgr.enterUndoContext(cUndo) 'only one step in Undo
for i=2 to iPage
with oVCur
.jumpToPage(i)
.jumpToStartOfPage()
.gotoStartOfLine(false) 'start of 1st line
.gotoEndOfLine(true) 'select line
s=.String 'text of selected line
end with
rem test for dashed line
s=replace(s, "-", "") 'remove all "-" from line
if s="" then oVCur.String="" 'it is clear dashed line so delete it
next i
rem remove empty last page
with oVCur
.jumpToStartOfPage()
.gotoStartOfLine(false)
.goToEnd(true)
s=.String 'text of last page
end with
dim calc as object
calc=CreateUnoService("com.sun.star.sheet.FunctionAccess")
s=calc.callFunction("REGEX", array(s, "[^:print:]", "", "g")) 'replace non-print characters
if s="" then oVCur.String="" 'there isn't printable character so delete line
undoMgr.leaveUndoContext()
oVCur.gotoRange(oPos, false) 'cursor to initial position (error at this line if cursor wasn't "focused" in document when macro started)
final:
if oDoc.hasControllersLocked() then oDoc.unlockControllers
End Sub
Theoretically there is some chance to add Enter at the end of page and then delete whole 1st dashedLine+Enter, but if the page ends with the Table and not text, then it is complicated to add new line under the Table, because the macro works with visible cursor and some big or wild jumping of visible cursor could do unexpected bugs, because the execution of macro code could be faster than moving of cursor. So the solution with empty line instead of dashed line seems better.