Ask Your Question

selecting background image with a macro in calc

asked 2016-12-23 13:59:09 +0200

gastcra gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-12-24 08:29:44 +0200

Ratslinger gravatar image

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.

edit flag offensive delete link more


Thanks, Ratslinger. Just what I needed!

gastcra gravatar imagegastcra ( 2016-12-24 12:35:21 +0200 )edit

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.

Ratslinger gravatar imageRatslinger ( 2016-12-25 02:48:47 +0200 )edit

Thanks again, Ratslinger. I made the fix.

gastcra gravatar imagegastcra ( 2016-12-26 18:46:51 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-12-23 13:59:09 +0200

Seen: 105 times

Last updated: Dec 24 '16