Text box created via macro disappear

I am using Writer macro to create a text box located on each bottom right corner of the page.

But when the text box is created, it disappears. Only the first one being created in page 1 appears.

Sometimes the text box will appear if I click around to find it, and I can also edit it, but once I click off it, it disappears again.

I have tried Arrange > ‘Bring to front’ and Arrange > ‘In front of object’, but it still stays disappeared.

For those text boxes being disappeared, only after I manually move or resize them, they stay appear finally and show being anchored to the corresponding pages.

Below is my code, please help me to find out what I have set wrong. Thank you very much.

dim oDoc as object, oVCur as object
oDoc=ThisComponent
oVCur=oDoc.CurrentController.ViewCursor

dim Page as object
dim oStyle as object
dim  pageStyle as String
pageStyle = oVCur.PageStyleName
oStyle = oDoc.StyleFamilies.getByName("PageStyles").getByName(pageStyle)
   
 	Dim myForm As Object
Dim myDimensionsForm As New com.sun.star.awt.Size
    Dim myPositionForm As New com.sun.star.awt.Point

for i=1 to Page 'loop to add text box to each page
			
	myForm = thisComponent.createInstance("com.sun.star.drawing.TextShape")

	'size of text box
	myDimensionsForm.Width  = 1700                                          
	myDimensionsForm.Height = 800                                         
	'position of each text box relative to the size of the page
	myPositionForm.X      = 18000                                       
 		myPositionForm.Y      = 28900 + (i-1)*oStyle.height + (i-1)*800/2                                    

	myForm.Size             = myDimensionsForm
	myForm.Position         = myPositionForm	
	myForm.rotateAngle      = 0
	myForm.AnchorType       = com.sun.star.text.TextContentAnchorType.AT_PAGE
	myForm.AnchorPosition.X = 0
	myForm.AnchorPosition.Y = 0

	thisComponent.drawPage.add(myForm)
	
	'each text box has string p#123, where 123 will be a variable in the future
	myForm.String = Chr(112)&Chr(35)&"123"

	
next i

for i = 1 to Page

Page is an Object type variable but not an Integer number in your code. You can not use it as a loop upper limit variable

Please read Andrew Pitonyak’s free macro books.
https://www.pitonyak.org/oo.php

From the OOME3.0 book:

  •         13.9.3.  Writer
    

Each Writer document contains a single draw page for the entire document. In Writer, the page is like a transparent layer containing drawing data on top of the standard document data.
Writer documents do not support the XDrawPagesSupplier interface, because they only contain a single draw page. They do, however, support the XDrawPageSupplier interface, which defines the single object method getDrawPage().

Therefore you can not get the Draw pages of the Text pages separatelly.

In fact, they do support XDrawPagesSupplier interface since https://gerrit.libreoffice.org/c/core/+/47681 from 2018 (LibreOffice 6.1), for uniformity. That interface will provide the same single draw page.

But yes, Writer’s Draw page is orthogonal to Writer’s pages.

Here is my approach based on the Andrew Pitonyak’s macro examples:
TextBoxMacro.odt (11.9 KB)

It seems as some Footer. There is real Footer and Header feature in the Page Styles. Maybe it is better to use them - without any macros.

Thank you Zizi64 for providing the macro example. It works! Thanks! I have read the details and found that each text box should anchor to its corresponding page, which I have not set in my code. To do so, add back " myForm.AnchorPageNo = i" in my coding and then all the text boxes created will stay appeared.