底下的程式碼可在標原有的標準工具列中新增一個按鈕,於文件開啟時載入,
於文件關閉時刪除。

先在VBE環境中,雙點 ThisWorkBook

Dim myButton1 As CommandBarButton   ' 此物件要宣告在一般區中
Private Sub Workbook_Open()    '開啟文件時要執行的事
   Set myButton1 = Application.CommandBars("Standard").Controls.Add
   With myButton1
         .Style = msoButtonCaption   '只顯示文字
         '.Style = msoButtonIcon       '只顯示小圖示
         '顯示文字和小圖示
         '.Style = msoButtonIconAndCaption msoComboLabel 
         .BeginGroup = True
         .Caption = "轉成圖表"
         .TooltipText = "轉成圖表"
         .FaceId = 159             '小圖示
         .Tag = "MyCustomTag"
         .OnAction = "MainVBA"     '設定按下此鍵時所要執行的巨集
   End With
End Sub

'關閉文件時,刪除該按鈕
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    myButton1.Delete          
End Sub

註:感謝yahoo 沙拉油先生的回應,當初為了要能刪除該按鈕,想破頭,
       但沒想到高手一出招,問題馬上迎刃而解。
       切記,button物件要宣告在 sub之外。

arrow
arrow
    全站熱搜

    ydmqqq 發表在 痞客邦 留言(0) 人氣()