I am trying to implement a page-scope counter (that is, a counter with the scope of one page) to count a number of elements in each page, dynamically. Is this possible in LO?
You will surely know that pages (like lines) aren’t fixed objects in a Writer document. They are generated on the fly to present the layout. In addition not every “element” created for a document is contained in “the text”. Frames and also shapes are child objects of ThisComponent.DrawPage and have their own text object properties. Frames and shapes have an anchor of one or another type defining their relation to the text flow…
If you exclusively want to count characters of the general text belonging to a specific page under the current layout, you may use the .String
property of the TextCursor returned by the following user function (BASIC):
Function getPageAsTextCursor() As Object
doc0 = ThisComponent
text0 = doc0.Text
tVC = doc0.CurrentController.GetViewCursor
tVCold = text0.createTextCursorByRange(tVC)
tVCold.GotoRange(tVC, False)
pgTC = text0.createTextCursorByRange(tVC)
tVC.JumpToStartOfPage()
pgTC.GotoRange(tVC.Start, False)
tVC.JumpToEndOfPage()
pgTC.GotoRange(tVC.End, True)
tVC.GotoRange(tVCold, False)
getPageAsTextCursor _
= pgTC
End Function
“The page” will be the one containing the logical .End
of the current ViewCursor.
Frames and shapes will not be regarded. I did not thoroughly research many details.
You may have to study programming for LibreOffice to more depth. Did you already find the famous texts by Andrew Pitonyak?
Thanks for the answer, and for the code! I really appreciate it. I haven’t looked very deeply into LO macro programming, but it seems I may have to.