I just want to point out to anyone here for similar tasks, I ended up recording a macro while manually adding a button and editing its properties, then deleting it.
While this did not work it did give me much better terms to search for, and led me to figure out how to do it.
I guess knowing where to start can be the hardest step.
Sub CreateButton()
col = 7
row = 20
oSheet = ThisComponent.currentcontroller.activesheet
oForm = oSheet.DrawPage.getForms().getByIndex(0)
oCell = oSheet.getCellByPosition(col, row)
'Create the button
oBut = CreateUnoService("com.sun.star.form.component.CommandButton")
oBut.Name = "But_" + col + "_" + row
oBut.Label = "RECV"
'Create the shape to contain it
oShape = ThisComponent.createInstance("com.sun.star.drawing.ControlShape")
oShape.Name = "CS_" & oBut.Name
'Add the shape to the page, set anchor and position, size
oShape.setControl(oBut)
oSheet.DrawPage.add(oShape)
oShape.Anchor = oCell
oShape.setSize(oCell.Size)
oShape.setPosition(oCell.Position)
'Add the event to handle clicks
aEvent = CreateUnoStruct("com.sun.star.script.ScriptEventDescriptor")
With aEvent
.AddListenerParam = ""
.EventMethod = "actionPerformed"
.ListenerType = "XActionListener"
.ScriptCode = "vnd.sun.star.script:Standard.Module1.ButtonClick?language=Basic&location=document"
.ScriptType = "Script"
End With
oForm.registerScriptEvent(oForm.Count-1, aEvent)
End Sub
Sub ButtonClick(oEvent as com.sun.star.awt.ActionEvent)
oSheet = ThisComponent.currentcontroller.activesheet
oForm = oSheet.DrawPage.getForms().getByIndex(0)
'Remove the event (not sure if this is required, but seems like it should be)
For i = 0 To oForm.count - 1
oFormEle = oForm.getbyindex(i)
If (oFormEle.Name = sButName) Then
oForm.revokeScriptEvent(i, "XActionListener", "actionPerformed", "")
Exit For
End If
Next i
'Find which button was clicked, perform some work then delete it
For i = 0 To oSheet.DrawPage.count - 1
oShape = oSheet.DrawPage.getbyindex(i)
If (oShape.Name = "CS_" & oEvent.Source.Model.Name) Then
oCell = oShape.Anchor.CellAddress
Print "Button clicked on Sheet " & oCell.Sheet & ", Col " & oCell.Column & ", Row " & oCell.Row
oShape.Dispose
Exit For
End If
Next i
End Sub