Progammatically Assign Macro to a Shape (on Calc Sheet)

I have been trying for quite a while to determine how exactly to assign a macro to a shape that resides on a Calc Sheet. As far as I can tell from the documentation there is no method/property which accomplishes this directly and the macro recorder only records up to the displaying of the assign macro dialog.

Having browsed around looking for the answer on the web a lot results talk of assigning macros to form controls with respect to dialogs or how to assign through the GUI. Another result was through the ‘ScriptEventDescriptor’ but again the example I came across uses a form to register the event which isn’t present on a Calc Sheet ( Register macro to Image Button - #2 by Ratslinger ) - I assume because it’s using a form control.

If I can assign a macro to a shape object through the Calc GUI surely there is way to do it programmatically - can anyone point me in the right direction?

Thanks in advance

Hello and welcome Scorpio721!
I see that you are a responsible person - before asking a question you really searched and tried to understand. I am glad! Lately this happens so rarely! So once again - welcome!
Since I see that you understand what you are asking, I will not tell you what this example does - you will understand it from the comments in the code.

EventToImg.ods (17.7 KB)

For beginners who will not understand, I will explain - you need to click the buttons several times in random order, then click on the emoticons and make sure that the behavior of the emoticons changes under the action of the macro attached to the button (I hope that I was able to confuse the explanation completely)

1 Like

… and didn’t find,
which should make us not so glad :expressionless:

yep.


to sumup (and have it somehow indexed for later searches) :

If  oShape.supportsService("com.sun.star.drawing.CustomShape") Then 

Dim aProperty(1) As New com.sun.star.beans.PropertyValue
	aProperty(0).Name = "EventType" : aProperty(0).Value = "Script"
	aProperty(1).Name = "Script" : aProperty(1).Value = "vnd.sun.star.script:Standard.Module1.NothingToDo?language=Basic&location=document"

	oShape.getEvents().replaceByName("OnClick", aProperty)

LibreOffice: CustomShape Service Reference