How to load an image as bitmap in a writer document with a macro

With the GUI i can load an image as a bitmap and assign a name to it. How can this be achieved with a macro, so it can be rerenced later as the background of a frame


Preferably the image is embedded in the document.

When editing a question (or an answer), please make clear what was changed.
Seems you appended frame.FillBitmapName = 'the_new_added_bitmap_name' without explaining the reason.
It may not work this way. Terminology develops, and misconceptions survive.
Did you read my answer?
Did you read Andrew’s text?
Please explain in what way the mentioned information was insufficient for you. After all I reported that my code actually worked - and that wasn’t a lie. I made the time to test.
Anyway: I feel sure you won’t succeed without using the BitMapTable service. The recommendation to use the myFrame.BackGraphic -property in LibreOffice V 6.1 or higher is from LibreOffice: BaseFrameProperties Service Reference.

Thanks for the API link, i was looking for that.

Sorry to caused confusion, there is a difference in skills between us (20.000 to 38), so it stems from ignorance of LO macros. I have no education in programming, it is very hard to get hold of the concepts of API, services, structs, frames, interfaces, UNO and so on, leave alone how these are related. Documentation with boxes and arrows starts at quite a skilled level, and is assuming you are already familiar with the principles. I work in a mixture of reading documentation, examples in the forum, MRI and trial and error.

By MRI i found that property FillBitmapName was identical to a manually set bitmap name, eg. the name ‘fence’ or ‘paper’ appeared. It brought the visual result wanted (Version: 6.4.6.2, and yes the image was added to the bitmap first )

Your approach is more elegant and up to date. Therefore i did revise the answer given to the barcode question.

I don’t feel sure to understand, but what Andrew Pitonyak described under 5.9.1 of his “Useful Macro Information” actually worked for me (slightly adapted) in a test.

The strange thing troubling me is that I need to create the BitmapTable also to get access to already loaded images. If such a table was created during a previous run of a respective Sub, and got inserted an image, this image is present next time when I “create” the table again.

I would expect to get access to the previously created BitmapTable in a similar way as to a DrawPage or to the TextTables container.

Sub demo()
doc = ThisComponent
bmt = doc.createInstance("com.sun.star.drawing.BitmapTable")
bmt.insertByName("jpg1", ConvertToURL("C:\Users\myFolderPathToTheImage\IMG_8283.jpg"))
REM A later attempt to insert an image under the same name int a newly "created" bmt will cause an error.
fr1 = doc.TextFrames.getByName("Frame1") REM The frame was created and named previously.
internalJpg1 = bmt.getByName("jpg1")
fr1.BackGraphic = internalJpg1
End Sub

Thanks Lupp. I tried about an identical code, without the ConvertToUrl , then it doesn.t work wthout an error message
In python uno.systemPathToFileUrl( filename)
Now I will try answer another question about inserting a barcode barcode question