Ask Your Question

Register macro to Image Button

asked 2020-08-01 13:35:24 +0200

Chucko gravatar image

updated 2020-08-06 13:12:08 +0200

Alex Kemp gravatar image

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 = ""
dp = Sheet.DrawPage                                                 ' Get the DrawPage
shape = ThisComponent.createInstance("")' Create a Shape 
ib = CreateUnoService("")    ' 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
aEvent = CreateUnoStruct("")
With aEvent
    .AddListenerParam = ""
    .EventMethod = "actionPerformed"
    .ListenerType = "XActionListener"
    .ScriptCode = scriptURL
    .ScriptType = "Script"
End With
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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-08-01 18:53:50 +0200

Ratslinger gravatar image

updated 2020-08-02 02:22:04 +0200


Have tested with my code and have a working set. For my sample see my answer in this post -> How to access the settings under the 'Events' tab of a form control to get / set the routines?

The answer has a link to the origin of my code used. It has a sample and in that sample the is a list of controls with associated Listener, Event Method and Control Event.

There are a couple of items wrong with your code. You have an incorrect listener (s/b - XMouseListener) and Event Method ( my prefrence - mouseReleased). I prefer "Mouse released". This way if you change your mind, before releasing the mouse button, remove the cursor from the button and then release. No execution.

Edit 2020-08-01:

For clarity, just added this code to the end of CreateButton sub (from original code provided in link in your question):

myScript = ""
oForm = oSheet.DrawPage.getForms().getByIndex(0)
myListener = "XMouseListener"
listenType = "mouseReleased"
nCount = oForm.getCount()
for x = 0 to nCount - 1
    oControl = oForm.getByIndex(x)
    if oControl.Name = "Button_1" then
        controlIndex = x
        exit for
    end if
    if x = nCount - 1 then
        MsgBox "Control not found"
        Exit sub
    end if
descriptor = New
With descriptor
    .ListenerType = myListener
    .EventMethod = listenType
    .AddListenerParam = ""
    .ScriptType = "Script"
    .ScriptCode = myScript
end with
oForm.registerScriptEvent(controlIndex, descriptor)

and added this for test:

Sub myHello
    MsgBox "Hello"
End Sub
edit flag offensive delete link more


Thanks @Ratslinger once again for your invaluable help and for the links to very useful additional info. Close examination of some of the search results I had previously found probably would have shown me that I was registering the wrong listener type. I need to learn to slow down and read more carefully. I find that I often miss such crucial things while reviewing answers- often assuming I've made big or obvious mistakes when my issues are more subtle.

Chucko gravatar imageChucko ( 2020-08-02 11:48:58 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-08-01 13:35:24 +0200

Seen: 25 times

Last updated: Aug 02