[Solved] getControl missing while searching Button name

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
amlbl
Posts: 9
Joined: Tue Jan 07, 2020 6:37 pm

[Solved] getControl missing while searching Button name

Post 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 4625 times
Last edited by Hagar Delest on Wed Feb 12, 2025 2:55 pm, edited 1 time in total.
Reason: tagged solved.
Open Office 4.1.7 on Windows 10
User avatar
Hagar Delest
Moderator
Posts: 33352
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: getControl missing while searching Button name

Post 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.
LibreOffice 25.2 on Linux Mint Debian Edition (LMDE Faye) and 24.8 portable on Windows 11.
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: getControl missing while searching Button name

Post by JeJe »

Try supportsService("com.sun.star.drawing.ControlShape")
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bidouille
Volunteer
Posts: 641
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: getControl missing while searching Button name

Post 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
amlbl
Posts: 9
Joined: Tue Jan 07, 2020 6:37 pm

Re: getControl missing while searching Button name

Post 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.
Open Office 4.1.7 on Windows 10
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: getControl missing while searching Button name

Post by JeJe »

Do you use MRI, you can easily inspect objects with it:

viewtopic.php?t=49294
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked