Macros 我如何称呼按钮?(OpenOffice电子表格宏)

Macros 我如何称呼按钮?(OpenOffice电子表格宏),macros,openoffice.org,Macros,Openoffice.org,我有一个在OpenOffice中启动宏的按钮。在宏中,我想更改按钮的名称。Excel的原始代码是 ActiveSheet.Shapes("PunchButton").select Selection.Characters.Text = "Punch In" 但第一行什么也没做。我已经在OpenOffice中检查了工作表,按钮的名称正确。我该怎么做呢?这里有一段代码,演示了如何更改所有按钮的标签和状态,但您需要的按钮除外 Sub clickCommandButton1 o

我有一个在OpenOffice中启动宏的按钮。在宏中,我想更改按钮的名称。Excel的原始代码是

    ActiveSheet.Shapes("PunchButton").select
    Selection.Characters.Text = "Punch In"
但第一行什么也没做。我已经在OpenOffice中检查了工作表,按钮的名称正确。我该怎么做呢?

这里有一段代码,演示了如何更改所有按钮的标签和状态,但您需要的按钮除外

Sub clickCommandButton1
    oPage = Thiscomponent.Sheets.getByName("Sheet1").getDrawPage
    iCount = oPage.getCount
    For i = 0 to iCount - 1
        oEle = oPage.getByIndex(i)
        oControl = oEle.getControl()
        If oControl.DefaultControl = "com.sun.star.form.control.CommandButton" Then
            ' Found command button - change label of other buttons '
            If oEle.Name <> "CommandButton1" Then
                oControl.Label = "Inactive"
                oControl.Enabled = False
            End If
        End If
    Next
End Sub 
子单击命令按钮1
oPage=Thiscomponent.Sheets.getByName(“Sheet1”).getDrawPage
iCount=oPage.getCount
对于i=0到i计数-1
oEle=oPage.getByIndex(一)
oControl=oEle.getControl()
如果oControl.DefaultControl=“com.sun.star.form.control.CommandButton”,则
'找到命令按钮-更改其他按钮的标签'
如果oEle.名称为“CommandButton1”,则
oControl.Label=“非活动”
oControl.Enabled=False
如果结束
如果结束
下一个
端接头

我会修改它以迭代所有按钮,但将内部if语句更改为“=”而不是“”(如果不需要,则删除禁用)。

多亏了Pax,这是我的工作代码。不确定它有多健壮,但对于讨论中的工作表,它可以工作。再次感谢Pax

sub testThis
    setButtonLabel("PunchButton", "hello")
    setButtonLabel("ReportButton", "hello")
end sub

sub setButtonLabel(controlName, label)
    oPage = ThisComponent.CurrentController.ActiveSheet.getDrawPage
    iCount = oPage.getCount
    For i = 0 to iCount - 1
        oControl = oPage.getByIndex(i).getControl
        If oControl.Name = controlName Then
            oControl.label = label
            exit sub
        End If
    Next
end sub

太好了,谢谢。我会检查一下,然后在代码运行时发布我的代码。