【问题标题】:How do I address a button? (OpenOffice Spreadsheet Macros)如何寻址按钮? (OpenOffice 电子表格宏)
【发布时间】:2009-02-26 00:36:10
【问题描述】:

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

    ActiveSheet.Shapes("PunchButton").select
    Selection.Characters.Text = "Punch In"

但是第一行什么也没做。我在 OpenOffice 中检查了工作表,并且按钮的名称正确。我如何获得它?

【问题讨论】:

    标签: macros openoffice.org


    【解决方案1】:

    有一个代码 here 的 sn-p,它显示了如何更改所有按钮的标签和状态,但您想要的按钮除外。

    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 
    

    我会修改它以遍历所有按钮,但将内部 if 语句更改为 '=" 而不是 ""(如果不需要,请删除禁用)。

    【讨论】:

    • 太好了,谢谢。我会检查它并在它工作时发布我的代码。
    【解决方案2】:

    感谢 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
    

    【讨论】: