使用LibreOffice Basic迭代控件

时间:2015-04-15 14:10:52

标签: vba controls libreoffice basic libreoffice-basic

我希望使用Basic迭代LibreOffice表单中的控件。

基本上,我想做这个代码在VBA中所做的事情。

Sub parcours_controles_formulaire()
    Dim cControl As Control
    Dim sLog As String
    sLog = "List of controls : " & vbCrLf 
    For Each cControl In FrmExemplesControles.Controls
        sLog = sLog & _
        cControl.Name & _
        " of type " & _
        TypeName(cControl) & vbCrLf
    Next cControl
    MsgBox sLog
End Sub

编辑:以下是我在Lyrl的帮助下找到的内容。那还不完全正确。我无法获得控件标签。

Sub iterate_forms_controls()
    Dim Dlg As Object
    Dim Controls As Object
    Dim cControl As Object
    Dim I As Integer
    Dim A As String

    DialogLibraries.LoadLibrary("Standard")
    Dlg = CreateUnoDialog(DialogLibraries.Standard.BoiteDeDialogue1)

    Controls = Dlg.Controls

    I = 0
    A = ""
    For Each cControl In Controls
        I = I + 1
        A = A & cControl.getImplementationName()
        'A = A & cControl  ' How to get back the label of cControl here ?
    Next cControl

    MsgBox "There is " & i & " controls in that form !" & A
End Sub

1 个答案:

答案 0 :(得分:1)

注意:此代码在OpenOffice中进行了测试。我相信它在LibreOffice中的工作方式相同。

控件位于绘图页面上。如果可能存在非控制绘图对象(箭头,形状,图像等),并且您希望仅对控件进行操作,则必须遍历所有绘制对象并测试每个对象作为控件:

Sub iterate_forms_controls()
    Dim oDP As Object : oDP = ThisComponent.drawpage
    Dim cControl As Object
    Dim i As Integer

    REM The oDP assignment above is for a standard form (a Writer document).
    REM If you are using a Calc document as a form you would instead write:
    REM oDP = ThisComponent.Sheets.getByName("SheetName").drawpage

    For i = 0 To oDP.Count - 1
    cControl = oDP.getByIndex(i)
        If cControl.supportsService("com.sun.star.drawing.ControlShape") Then
            'Do something
        End If
    Next i
End Sub

编辑:我用XRay查看了对象“cControl”。查看属性,看起来没什么用处。然后我去了方法并找到了一个方法“getModel”。双击getModel到XRay那个方法,找到名为“我给”复选框的“Label”。活泉! (我使用过其他具有某些属性的对象,只能通过“模型”访问;它不是一个直观的地方。)

所以试试这个:

For Each cControl In Controls
    I = I + 1
    A = A & cControl.getModel.Label
Next cControl