In a Calc Workbook named Tracker.ods, using the code provided as an answer to the question I asked here, I created an Image Button on a sheet . But, despite numerous searches, I cannot find the code needed to assign one of my macros named “EditImageURL” to that button when it is created. All of the macro code resides in the Standard container of the Workbook itself in a Module named “Tracker”. I’d like my “EditImageURL” macro to be invoked when the Image Button is clicked.
Here is the relevant code that I cobbled together from various posts:
scriptURL = "vnd.sun.star.script:Standard.Tracker.EditImageURL?language=Basic&location=document"
dp = Sheet.DrawPage ' Get the DrawPage
shape = ThisComponent.createInstance("com.sun.star.drawing.ControlShape")' Create a Shape
ib = CreateUnoService("com.sun.star.form.component.ImageButton") ' Create the Button
' (snip) ... set size and position of the shape)
shape.setControl(ib) ' Add the Button Control to the Shape
dp.add(shape) ' Add the shape to the DrawPage
' Register the Macro script to this new Button
oForm = dp.getForms().getByIndex(0) ' find index within the container
nIndex = -1
For i = 0 To oForm.getCount() - 1 step 1
If EqualUnoObjects(ib, oForm.getByIndex(i)) Then
nIndex = i
Exit For
End If
Next
aEvent = CreateUnoStruct("com.sun.star.script.ScriptEventDescriptor")
With aEvent
.AddListenerParam = ""
.EventMethod = "actionPerformed"
.ListenerType = "XActionListener"
.ScriptCode = scriptURL
.ScriptType = "Script"
End With
' BELOW DOESN'T WORK AS DESIRED. NOTHING GETS REGISTERED.
oForm.registerScriptEvent(nIndex, aEvent)
The Image Button is created on the Sheet as desired. But, as noted in the last comment above, no script is registered for any event associated with the button. I suspect either that I have registered the event against the wrong object, or that I have selected an EventMethod
that is not appropriate for an Image Button control. Search as I might, I cannot find a list of valid EventMethods. If I manually assign my macro to the button by right-clicking it and using Control Properties
> Events
> Mouse Button Pressed
> Macro
my macro is invoked and runs as desired. But I cannot seem to make the assignment via the above code.
EDIT #1: Further research indicates that the EventMethod I need to register is “mousePressed” and when the above code is modified to read .EventMethod = "mousePressed"
then the desired macro is, indeed registered. But the code is not invoked when I click the button. So, as noted above, I suspect I’m registering the event to the wrong Object.