クイックアクセスツールバーに追加したボタンをクリックしたときにポップアップメニューを表示するマクロです。
1. Officeファイルを開きます。
2. 標準モジュールに下記コードを貼り付けます。
Option Explicit
Private Const PopupBarName As String = "MyPopupMenu" 'コマンドバー名(適宜変更)
Public Sub ShowPopupMenu()
'メニュー内容(Caption等)や実行するプロシージャは適宜変更
On Error Resume Next
Call DelPopup
With Application.CommandBars.Add(Name:=PopupBarName, Position:=msoBarPopup, Temporary:=True)
With .Controls.Add(Type:=msoControlButton, Temporary:=True)
.Caption = "Menu1"
.FaceId = 2580
.OnAction = "MenuProc"
End With
With .Controls.Add(Type:=msoControlPopup, Temporary:=True)
.Caption = "Menu2"
With .Controls.Add(Type:=msoControlButton, Temporary:=True)
.Caption = "SubMenu1"
.FaceId = 2580
.OnAction = "MenuProc"
End With
With .Controls.Add(Type:=msoControlEdit, Temporary:=True)
.Caption = "SubMenu2"
.FaceId = 2580
.OnAction = "MenuProc"
End With
With .Controls.Add(Type:=msoControlComboBox, Temporary:=True)
.Caption = "SubMenu3"
.FaceId = 2580
.OnAction = "MenuProc"
.AddItem "Item1"
.AddItem "Item2"
.AddItem "Item3"
.AddItem "Item4"
.AddItem "Item5"
End With
End With
With .Controls.Add(Type:=msoControlButton, Temporary:=True)
.BeginGroup = True '区切り線
.Caption = "Menu3"
.FaceId = 2580
.OnAction = "MenuProc"
End With
With .Controls.Add(Type:=msoControlButton, Temporary:=True)
.Caption = "Menu4"
.FaceId = 2580
.OnAction = "MenuProc"
End With
.ShowPopup
End With
On Error GoTo 0
End Sub
Private Sub MenuProc()
Select Case Application.CommandBars.ActionControl.Type
Case msoControlButton
MsgBox Application.CommandBars.ActionControl.Caption
Case msoControlEdit, msoControlComboBox
MsgBox Application.CommandBars.ActionControl.Text
End Select
Call DelPopup
End Sub
Private Sub DelPopup()
Dim c As Office.CommandBar
For Each c In Application.CommandBars
If c.Name = PopupBarName Then c.Delete
Next
End Sub
参考Webページ:
http://www.rondebruin.nl/qat.htm