Selecting background image with a macro in calc

I am trying to select a background image using a macro in Calc. I recorded a macro in which I selected the background image using the Navigator and moved it to the foreground. I then did an operation on the image and then closed the macro. The recorded macro doesn’t do anything. I noticed that some of the commands in the recorded macro are commented out. I uncommented them but it still doesn’t work.

Is there a way to do this?

Recorded macros aren’t always the way to go. Tried it & saw it’s missing image name but even with that couldn’t get it to work. Put together this macro:

sub ImageForeground
 rem ----------------------------------------------------------------------
 dim oDrawPage   as object
 dim oImage as object
 dim iCount as Integer
rem ----------------------------------------------------------------------
rem get access to the document
  oDrawPage = ThisComponent.getSheets().getByName("YOUR-SHEET-NAME-HERE").getDrawPage()
  iCount = oDrawPage.Count
rem search for proper image
  for x = 0 to iCount
    oImage = oDrawPage.getByIndex(x)
    If oImage.Name = "IMAGE-NAME-HERE" Then
rem image found - bring to foreground
	  oImage.LayerID = 0
	  Exit Sub
    End If
  Next x
rem Image not found - error
   MsgBox "Object not Found"
end sub

You need to enter two items where indicated - the sheet name (same as tab) and image name (from navigator). The line oImage.LayerID = 0 brings the image to the foreground. If this is set to a 1, it would send the image to the background.

Thanks, Ratslinger. Just what I needed!

Sorry for the inconvenience but there is a small error in the code. If a wrong image name is entered, an out-of-bounds exception will be thrown. Otherwise all else is OK. To fix, change this line for x = 0 to iCount to for x = 0 to iCount -1. That’s it.

Thanks again, Ratslinger. I made the fix.