使用 LibreOffice Basic 迭代控件
Iterate over Controls with 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
注意:此代码已在 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"。查看了属性,没有任何有用的东西。然后我去了methods,找到了一个方法"getModel"。双击 getModel 以对该方法进行 XRay,然后发现 "Label" 具有我为复选框指定的名称。哇! (我使用过其他对象,这些对象具有只能通过 "model" 访问的某些属性;这不是一个直观的地方。)
所以试试这个:
For Each cControl In Controls
I = I + 1
A = A & cControl.getModel.Label
Next cControl
我想使用 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
注意:此代码已在 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"。查看了属性,没有任何有用的东西。然后我去了methods,找到了一个方法"getModel"。双击 getModel 以对该方法进行 XRay,然后发现 "Label" 具有我为复选框指定的名称。哇! (我使用过其他对象,这些对象具有只能通过 "model" 访问的某些属性;这不是一个直观的地方。)
所以试试这个:
For Each cControl In Controls
I = I + 1
A = A & cControl.getModel.Label
Next cControl