0

我想用 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
4

1 回答 1

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 那个方法,然后找到“标签”,它的名称是我给复选框指定的名称。哇!(我使用过其他具有某些属性只能通过“模型”访问的对象;这不是一个直观的地方。)

所以试试这个:

For Each cControl In Controls
    I = I + 1
    A = A & cControl.getModel.Label
Next cControl
于 2015-04-15T16:44:09.793 回答