In a (basic) macro, how can I make a Button resize to the size of Cell it's anchored to when the Cell's size changes?

I have written a (basic) macro that I use to create a Control Button attached and sized to the currently selected Cell.

I would like for the Button to always have the same size as the Cell.

I have tried using “ResizeToCell = TRUE” on the Shape object that contains the Button, but that’s not doing it.

Here’s is the code I’m using:

  ' Create and configure a Command Button with Type=URL
  oButton = oDoc.createInstance("com.sun.star.form.component.CommandButton")
  oButton.ButtonType      = com.sun.star.form.FormButtonType.URL
  oButton.Name            = sButtonLabel + "_Button"
  oButton.Label           = sButtonLabel
  oButton.TargetURL       = sURL
  oButton.FocusOnClick    = false
  oButton.Repeat          = false
  obutton.BackgroundColor = RGB(255, 255, 215) ' ***** WHY DOES THIS CHANGE (DARKEN) AFTER I CLICK ON IT??? *****
  obutton.TextColor       = RGB(0, 150, 200)

  ' Create and configure a FontDescriptor for the Button and apply it
  oFontDesc              = oButton.FontDescriptor
' oFontDesc              = oDoc.createInstance("com.sun.star.awt.FontDescriptor")
  oFontDesc.Name         = "Arial"
  oFontDesc.Height       = 12
  oFontDesc.Weight       = 175     ' This is a PERCENTAGE
  oButton.FontDescriptor = oFontDesc

  oShape                = oDoc.createInstance("com.sun.star.drawing.ControlShape")
  oShape.Name           = oButtonLabel + "_Shape"
  oShape.Control        = oButton             ' Set the Button as the Control in the Shape
  oSheet.DrawPage.add(oShape)                 ' Add the Shape (that contains the Button) to the Sheet
  oShape.Size           = oSelection.Size     ' Size the Shape with the Size of the selected Cell
  oShape.Position       = oSelection.Position ' Position the Shape into the selected Cell
  oShape.Anchor         = oSelection          ' Anchor the Shape to the selected Cell
  oShape.ResizeWithCell = true                ' Resize the Shape every time the Cell size changes???
  oShape.SizeProtect    = true                ' Don't allow the user to resize the Shape
  oShape.MoveProtect    = true                ' Don't allow the user to move the Shape

ResizeWithCell = true and SizeProtect=true contradict.

Make the cell itself a button: http://forum.openoffice.org/en/forum/download/file.php?id=1261

Thank you, Regina. Setting SizeProtect=false fixed the problem.

I had thought SizeProtect=true only prevented the user from manually resizing the button, like dragging the edges of the button itself, if that were possible.

This is my first macro that really does anything much. Many thanks for helping me along the learning process.

See also the recent revival of: I have a basic macro that creates a button on a calc sheet. When the button is created it's anchor is set to cell. When the spreadsheet is saved, closed & re-opened the anchor changes to page, what am I doing wrong? - #6 by Lupp

What is explained there is valid for any FormControl, not just for buttons.