Replace text with image

I’d like to replace specified text (e.g., ###) with a specific image. I haven’t found how to do this, and would appreciate advice.

As a subsidiary issue, the image will recur many times in the document. Is there a way to incorporate it that will take less space - e.g. includes only one copy, but links to it, so that visually the image is present everywhere?

Hello @EvilOverlord,

To embed an image into your LibreOffice document, you could use the Basic macro EmbedImage() provided in the code section below.

To find all occurrences of a specified string pattern such as “###” within the current Writer document, and replace all the found occurrences with a previously embedded image, you could use the Basic method Writer_replaceAll_With_Bitmap() presented also in the code section below:

The embedded image will be inserted into your normal text flow using Anchor Type = AS_CHARACTER, so that the image will become part of the text flow.

Example call:

EmbedImage( "file:///home/user/Desktop/my_image.png", "My_Embedded_Image" )
Writer_replaceAll_With_Bitmap( "###", "My_Embedded_Image" )

Code:

Function EmbedImage( strImageFileURL As String, strImageName as String, Optional oDocument ) As String
REM Embed an image into a LibreOffice document*.
REM <strImageFileURL> : URL or Full Path to the Image file to be embedded.
REM						Will cause an Error if the URL is invalid, or if the URL points to an unsupported image file.
REM						Pass "" here, in order to Delete the embedded bitmap named <strImageName>.
REM    <strImageName> : Name to uniquely identify this image in the BitmapTable; usually the filename without extension.
REM       <oDocument> : [OPTIONAL] 	The document to embed the image in. [DEFAULT]= The current document.
REM						*For Base, this argument is required and must be a FormDocument in Design Mode ( not the DatabaseDocument ).
REM Returns the internal image URL to the embedded image ( vnd.sun.star.graphicObject ).
	If IsMissing( oDocument ) Or IsEmpty( oDocument ) Or IsNull( oDocument ) Then oDocument = ThisComponent
	Dim oBitmapTable As Object
	oBitmapTable = oDocument.createInstance( "com.sun.star.drawing.BitmapTable" )
	If Not IsNull( oBitmapTable ) Then
		If oBitmapTable.hasByName( strImageName ) Then
			If strImageFileURL = "" Then
				oBitmapTable.removeByName( strImageName )
			Else
				oBitmapTable.replaceByName( strImageName, strImageFileURL )
			End If
		Else
			oBitmapTable.insertByName( strImageName, strImageFileURL )
		End If
		EmbedImage = oBitmapTable.getByName( strImageName )
	End If
End Function

Sub Writer_replaceAll_With_Bitmap( strSearchPattern As String, strBitmapName As String )
REM Finds all occurrences of the specified string pattern in the current Writer document,
REM and replaces the found occurences with a previously embedded bitmap image.
REM <strSearchPattern> : String to be found/replaced within the current Writer document. Supports RegExp.
REM    <strBitmapName> : Name of a bitmap image embedded in the current Writer document.
REM See: https://ask.libreoffice.org/t/replace-text-with-image/30514	
	Dim oDocument As Object : oDocument = ThisComponent
	Dim oBitmapTable As Object
	oBitmapTable = oDocument.createInstance( "com.sun.star.drawing.BitmapTable" )
	If oBitmapTable.hasByName( strBitmapName ) Then 
		Dim sGraphicURL As String 		: sGraphicURL = oBitmapTable.getByName( strBitmapName )
		Dim oSearchDescriptor As Object	: oSearchDescriptor = oDocument.createSearchDescriptor()
		oSearchDescriptor.setSearchString( strSearchPattern )
		oSearchDescriptor.SearchRegularExpression = True	REM Set to <False> for normal search.
		Dim oSearchResults As Object	: oSearchResults = oDocument.findAll( oSearchDescriptor )
		Dim oFound As Object
		Dim i As Integer
		For i = 0 To oSearchResults.getCount() - 1
			oFound = oSearchResults.getByIndex( i )
			Dim oGraphic As Object
			oGraphic = oDocument.createInstance( "com.sun.star.text.TextGraphicObject" )
			oGraphic.GraphicURL = sGraphicURL
			oGraphic.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
			oFound.Text.insertTextContent( oFound, oGraphic, True)
		Next i
	End If
End Sub

With Regards, lib

Thanks. This looks a little more intensive than I had anticipated, but seems a viable solution, so I’ll test it out.

So, I’d either use fixed images and search strings, or use a dialog box to get inputs and then execute the calls, correct?

Got the following error in the embed image function. (Maybe because I’m on Windows, and not using the right file path?):
BASIC runtime error.
An exception occurred
Type: com.sun.star.container.NoSuchElementException
Message: .