none
動態產生Command Button RRS feed

  • 問題

  •  

    請問我該如何透過Macro,動態的產生Command Button呢?因為我執行下列程式時,到了「Set myCmdBtn」這段時會出現「此處需要物件」的錯誤

    Office Ver.:2003

    OS:Win XP

     

        Dim myCmdBtn() As Object
        Dim iBtnCnt As Integer
        Dim iLoopA As Integer

        iBtnCnt = 5

        ReDim myCmdBtn(iBtnCnt)

        For iLoopA = 0 To iBtnCnt - 1
            Set myCmdBtn(iLoopA) = Controls.Add("Forms.CommandButton.1", "cmdBtn" & iLoopA)

            myCmdBtn(iLoopA).Top = 500
            myCmdBtn(iLoopA).Left = (10 * iLoopA) + 100
            myCmdBtn(iLoopA).Width = 80
            myCmdBtn(iLoopA).Caption = iLoopA
        Next

    2008年6月25日 上午 12:06

解答

  • 線上手冊範例看過嗎?

    ms-help://MS.EXCEL.DEV.12.1028/EXCEL.DEV/content/HV10075314.htm
    [Visual Basic for Applications]
        Dim myBar As Office.CommandBar
        Dim imgSource As Office.CommandBarButton
        Dim myControl1 As Office.CommandBarButton
        Dim myControl2 As Office.CommandBarButton
        ' Add new command bar.
        Set myBar = CommandBars.Add(Name:="Custom", Position:=msoBarTop, Temporary:=True)
        ' Add 2 buttons to new command bar.
        With myBar
            .Controls.Add Type:=msoControlButton
            .Controls.Add Type:=msoControlButton
            .Visible = True 
        End With
        ' Paste Bold button face and set State of first button.
        Set myControl1 = myBar.Controls(1)
        Set imgSource = CommandBars.FindControl(msoControlButton, 113)
        imgSource.CopyFace
        With myControl1
            .PasteFace
            .State = msoButtonUp
        End With
        ' Paste italic button face and set State of second button.
        Set myControl2 = myBar.Controls(2)
        Set imgSource = CommandBars.FindControl(msoControlButton, 114)
        imgSource.CopyFace
        With myControl2
            .PasteFace
            .State = msoButtonDown
        End With
    

    T.L. Cheng
    2009年9月26日 上午 04:13
    版主