Page 1 of 1

[Solved] getControl missing while searching Button name

Posted: Tue Feb 11, 2025 12:19 pm
by amlbl
In a fairly complex worksheet I have a number of similar push buttons.
They all have a macro on the mouse button released event.
In this Sub I try to retrieve the name of the calling push button (with code borrowed from Pitonyak).
For certain shapes that are passed through for this, there appears to be no getControl method.
I have already tried in vain to get around this problem with the 'On Error'.
Question: How can I determine whether a getControl method exists?
Attached is the Sub.
Thanks in advance for your suggestions on this.

Code: Select all

Sub ButtonCall(x)
  Dim oButton 	' Button that was used to call the handler.
  Dim oModel 	' The model for the button.
  Dim oShape 	' The underlying button shape.
  Dim oControl
  Dim I, J As Long ' Generic index variable.
  Dim bFound As Boolean ' True after find the matching shape.
  Dim Naam as String
  
' First, get the button used to call this routine. Save the button's model.
  oButton = x.Source
  oModel = oButton.getModel()
  Naam = "Fout" 
  ' Iterate through the sheets en hun controls
  I = ThisComponent.getDrawPages().getCount()
  bFound = False
  Do While (i > 0 AND NOT bFound)
    I = I - 1
    oShape = ThisComponent.getDrawPages().getByIndex(I)
    J = oShape.Count
    Do While (J > 0 AND NOT bFound)
      J = J - 1
      On Error GoTo SkipIt
      'ErrorLog("Buttoncall: I=" + I + ", J=" + J, FALSE)
      oControl = oShape.getByIndex(J).getControl()     '<<<<<<<<<<<<<<<<<<<<<<<<<<<
      Naam = oControl.Name
      bFound = EqualUNOObjects(oControl, oModel)
      SkipIt:
    Loop
  Loop
 
  If bFound Then
    ' I = ButtonDispatcher(Naam)
    Msgbox Naam
  Else 
    ErrorLog("Buttoncall opgeroepen, maar geen naam van de drukknop gevonden")
  End If
  
End Sub
runtimefout.png
runtimefout.png (15.63 KiB) Viewed 4689 times

Re: getControl missing while searching Button name

Posted: Tue Feb 11, 2025 12:24 pm
by Hagar Delest
Please don't upload attachment for such question. I've deleted it and put its content in your post (the code and the screenshot).
Topic moved from Beginner section to Macros section too.

Re: getControl missing while searching Button name

Posted: Tue Feb 11, 2025 3:47 pm
by JeJe
Try supportsService("com.sun.star.drawing.ControlShape")

Re: getControl missing while searching Button name

Posted: Tue Feb 11, 2025 3:56 pm
by Bidouille
amlbl wrote: Tue Feb 11, 2025 12:19 pm In this Sub I try to retrieve the name of the calling push button
I don't understand why you go through the DrawPages layer.
The name is accessible through the model:

Code: Select all

Sub Snippet(Optional oInitialTarget As Object)
  Dim oSource As Variant
  Dim oModel As Variant
  Dim sName As String

  oSource = oInitialTarget.Source
  oModel = oSource.getModel()
  sName = oModel.Name
  
End Sub

Re: getControl missing while searching Button name

Posted: Wed Feb 12, 2025 12:03 pm
by amlbl
Dear Bidouille,
Thank you very much. You are absolutely right.
I was misled by the way Pitonyak worked.
I am closing the question.
Apologies from a beginner.

Re: getControl missing while searching Button name

Posted: Wed Feb 12, 2025 12:29 pm
by JeJe
Do you use MRI, you can easily inspect objects with it:

viewtopic.php?t=49294