BASIC runtime error Incorrect property value - getByName

Hi, we just upgraded from LibreOffice 5 to 6 and we’re now getting an error with one of our custom macros.
I didn’t write this code, so I don’t know much about it, but would appreciate any help.
Here’s the function:

' get the long name of the internal draft image
' if it isn't already embedded, do that first 
function getDraftImage(oDoc as Object) as string
	dim imageSource as string, imageName as string
	dim imageURL
	
	imageName = "draftImage.emf"
	imageSource = "O:\FORMS\TMaOOoGlobal\"
	
	oDoc = ThisComponent
	
	Dim oBitmaps
	oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )

	if oBitmaps.hasByName(imageName) then
		' it already exists, just get the long name
		getDraftImage = oBitmaps.getByName(imageName)
	else
		' insert it and return the new long name
		imageSource = imageSource & imageName
		imageURL = convertToUrl(imageSource)
		getDraftImage = LoadGraphicIntoDocument( oDoc, imageUrl, imageName )
	end if
end function

And the error is occurring on this line:

getDraftImage = oBitmaps.getByName(imageName)

Are you sure the getDraftImage function has always been declared as string? If I were BASIC, I would also be outraged if I was forced to push an object into a primitive string. Try to change the description of the function to As Variant, maybe it will help.

Thanks for the reply.
That got me passed getDraftImage(), but now it’s crashing inside LoadGraphicIntoDocument() with the same error at line:
cNewUrl = oBitmaps.getByName( cInternalName )
I also changed this function from string to Variant.

Function LoadGraphicIntoDocument( oDoc As Object, cUrl$, cInternalName$ ) As Variant
  Dim oBitmaps
  Dim cNewUrl As String

  oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )

  ' Add an external graphic to the BitmapTable of this document.
  oBitmaps.insertByName( cInternalName, cUrl )
   
  ' Now ask for it back.
  ' What we get back is an different Url that points to a graphic
  '  which is inside this document, and remains with the document.
  cNewUrl = oBitmaps.getByName( cInternalName )
   
  LoadGraphicIntoDocument = cNewUrl

Install the XrayTool to examine the properties and methods of the programming objects.

https://berma.pagesperso-orange.fr/index2.html

This code immediately seemed familiar to me. As I recall, this is “Listing 5.25: Insert the image into the internal bitmap table” from chapter “5.9.1. Danny Brewer embeds an image” There is usually no reason to doubt the code that was taken from Pitonyak’s book. I think the error is not here. There may be an error in your data. Can you show the whole project, not just fragments that are in doubt?

Thanks JohnSUN, that sounds like a book the author of our macros would have used. I don’t think I can post the whole project with the 1,000 chr. limit. I think I’ve fixed the 2 issues above.
I’m now stuck here: If oStyle.BackGraphicURL = “” Then in the Watermark Sub.
Gives “Data type mismatch”

SUB 1:

sub Watermark
	Dim i as integer
	Dim oDoc as Object, oStyles As Object, oStyle As Object
	Dim internalImage
 
	oDoc = ThisComponent
	oStyles = oDoc.StyleFamilies.getByName("PageStyles")
		
	' get the internal name of the image file (embed it if required)
	internalImage =  getDraftImage(oDoc)

	For i = 0 To oStyles.count - 1
		oStyle = oStyles(i)

		' toggle the watermark
		If oStyle.BackGraphicURL = "" Then
			oStyle.BackGraphicLocation = 5
			oStyle.BackGraphicURL = internalImage
			oStyle.BackGraphicFilter = "EMF - Enhanced Metafile"
		Else
			oStyle.BackGraphicLocation = 0
			oStyle.BackGraphicURL = ""
			oStyle.BackGraphicFilter = ""
		End if
	Next i
end sub

function 1:

function getDraftImage(oDoc as Object) as Variant
	dim imageSource as string, imageName as string
	dim imageURL
	
	imageName = "draftImage.emf"
	imageSource = "O:\FORMS\TMaOOoGlobal\"
	
	oDoc = ThisComponent
	
	Dim oBitmaps
	oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )

	if oBitmaps.hasByName(imageName) then
		' it already exists, just get the long name
		getDraftImage = oBitmaps.getByName(imageName)
	else
		' insert it and return the new long name
		imageSource = imageSource & imageName
		imageURL = convertToUrl(imageSource)
		getDraftImage = LoadGraphicIntoDocument( oDoc, imageUrl, imageName )
	end if
end function

Function 2:

Function LoadGraphicIntoDocument( oDoc As Object, cUrl$, cInternalName$ ) As Variant
  Dim oBitmaps
  Dim cNewUrl As Variant

  oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )

  oBitmaps.insertByName( cInternalName, cUrl )
   
  cNewUrl = oBitmaps.getByName( cInternalName )
   
  LoadGraphicIntoDocument = cNewUrl
End Function

I have additional questions: do you run the Watermark macro from a text document Writer?

Does the “O:\FORMS\TMaOOoGlobal” folder really have a “draftImage.emf” file and is it good?

Drive O: is it a network folder? Do you have access to it?

What version of office and operating system are you using?

Yes, the watermark macro is run from a Writer doc.
FYI, all this code was running fine last week when we where on Libre 5. It only stopped working after upgrading to 6.
Yes, the O: drive exists & has the draftImage.emf.

In fact if I remove:

If oStyle.BackGraphicURL = "" Then

And the else section, the draft watermark images gets inserted fine.

If I do:

Dim myStr as string
myStr = oStyle.BackGraphicURL

I still get “Data type mismatch”, but if I run:

Xray oStyle

It says the BackGraphicURL property is a string…

Yes, nothing stands still, everything changes. If you had to change the styles of pages in previous versions and now, then you will notice that there are now NONE and many other buttons for the background. To insert a picture, you now need to press the Bitmap button. This will revive all properties that have now begun to behave “wrong”. Unfortunately, I do not yet know how to perform this action from Basic. Perhaps someone who reads our discussion knows how to do this?