Use a macro to place something at the end of a paragraph

I need to place an object (in this case it’s a textbox created with Insert>Text Box at the end of a paragraph using a macro. It’s possible with the GUI by right-clicking and clicking “Position and Size” and setting the Vertical position to “Bottom” to “Entire Paragraph Area”. Is there a way to do this in a macro?

My version info is:
Version: 7.5.7.1 (X86_64) / LibreOffice Community
Build ID: 47eb0cf7efbacdee9b19ae25d6752381ede23126
CPU threads: 4; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: CL threaded

Thanks in advance for any responses.

Not answering to the question. Are you sure you need a text box? A text box is a Drawing Object, a geometric shape, which has limited possibilities, notably in formatting. The “correct” container for text is a frame. A frame provided a full-fledged independent sub-document, not larger then a page though. A frame can be controlled by a frame style while drawing objects like text boxes can’t and must be manually formatted and positioned.

So, instead of a text box + macro, you should consider a frame controlled by a common style. This would provide the automation you’re looking for. But, yes, frame styles are difficult to tame, even for experts. And above all, they don’t tolerate any direct formatting if you expect stability.

(Not answering the question either)
but if you post a .odt example of you expected result, it may/will motivate the macro gurus here to show you the exact way :expressionless:

This TextBox isn’t only simple object and has more properties, but base macro is here

Sub insertTextShape
	dim oDoc as object, oTextBox as object, oVCur as object
	oDoc=ThisComponent
	oVCur=oDoc.CurrentController.ViewCursor 'visible cursor
	oVCur.gotoEndOfLine(false) 'go to end of current line
	oTextBox=oDoc.createInstance("com.sun.star.drawing.TextShape")
	oDoc.Text.insertTextContent(oVCur, oTextBox, false) 'insert TextBox to end of current line
	with oTextBox 'properties of TextBox
		.setPosition(createPoint(100,100))
		.setSize(createSize(2000,200))
		.AnchorType=com.sun.star.text.TextContentAnchorType.AS_CHARACTER
		.VertOrient=com.sun.star.text.VertOrientation.BOTTOM
		.VertOrientRelation=0
		.LineStyle=1
		.LineWidth=10
		.String="Some text"
	end with
End Sub

@ajlittoz and @KamilLanda, it seems you both helped me progress. Indeed, it is a “Frame” that I’m looking for, and it turns out that putting it in “As Character” is the only way of reliably achieving my objective.

If you want it “in-line” as the last character of the paragraph, indeed As character is the only way but you must insert it manually.

If you want it “close to the end of paragraph”, a To paragraph frame with adequate position parameters will do the job automaticaly. And such a frame can be recorded in an AutoText so that insertion is no pain.

Actually, I want it immediately below the paragraph. My first attempt at achieving this was to manually put it in “To Paragraph” at the bottom of “Entire Paragraph Area”, but I discovered that Writer was glitchy and if I did this to my first paragraph and then tried to do it again to my second paragraph, the second frame would not anchor to the second paragraph at all, which ChatGPT said is a known unresolved issue, so I’ve resorted to using “As Character” at the end of the paragraph and trying to make its width exactly the width of the page minus the left and right page margins minus the effective left and right margins of the paragraph, and there is the rub: reliably determining the effective left margin of the paragraph. My attempt to achieve this is at this other thread

If you don’t anchor to the second paragraph, your code is buggy.

As character won’t work because it is not intended to work that way. First of all, you must add a line break so that you start at next line.

The crucial point is: what do you want to put inside this frame?

Actually, I was not adding it programmatically, but manually, so my code has nothing to do with it.

So, describe your procedure step by step. Using frames is tricky and you easily stumble into a trap from which there is no escape.

With the cursor in the paragraph, Insert>Frame>Frame... set the desired height and manually set the width to the width between the page margins. Leave Anchor set “To paragraph”. Horizontal position “Center” to “Entire Paragraph area”. Vertical position “Bottom” to “Entire Paragraph area”. Then “OK”. Should that land me in a trap?

Apart from the Bottom bug, my separate frames are anchored to separate paragraphs.

My config: Fedora 43, KDE Plasma desktop, LO 25.8.2.2

Not related to the problem: you can leave the width computation to Writer by ticking Relative to Entire paragraph area and setting Width to 100% (in that order).

Can you elaborate on what you mean by “the Bottom bug”?

You mentioned it yourself:

A frame is not inserted below the paragraph but at some distance from the top. I already filed a bug report about it but I don’t remember the bug number.


The clarification you provided in your other question paves the way for an easy solution.