即効テクニック |
コマンドバーをVBAから操作するとき、時としてある機能が属するコマンドバーの名前が知りたいときなどがあります。そんな時に一覧があると役にたちます。 サンプルでは全コマンドバーの名称及びその各種属性、及びそのコマンドバーに属する機能の一覧を作成します。
Sub GetCmdBarList() Dim MyCtrl As CommandBarButton For i = 1 To CommandBars.Count With CommandBars(i) 'コマンドバーの属性をシートに書き込み Cells(1, i).Value = .Name Cells(2, i).Value = .NameLocal Cells(3, i).Value = .AdaptiveMenu'97はこの行をコメントアウト Cells(4, i).Value = .BuiltIn Cells(5, i).Value = .Context Cells(6, i).Value = .Index Cells(7, i).Value = .Position Cells(8, i).Value = .Protection Cells(9, i).Value = .RowIndex Cells(10, i).Value = .Type Cells(11, i).Value = .Top For x = 1 To .Controls.Count With .Controls(x) '各ボタンの属性をセルに書き込み Cells((x - 1) * 4 + 12 + 1, i).Value = .Caption Cells((x - 1) * 4 + 12 + 2, i).Value = .ID Cells((x - 1) * 4 + 12 + 3, i).Value = .Type 'Typeプロパティーの値により、FaceIDを取得して貼り付け If .Type = msoControlButton Then Set MyCtrl = CommandBars(i).Controls(x) MyCtrl.CopyFace Cells((x - 1) * 4 + 12 + 4, i).Select ActiveSheet.Paste Set MyCtrl = Nothing End If End With Next x End With Next i End Sub
このサンプルにより作成された表の利用は、例えばシート上にあるワードアートの再編集のためにテキスト編集画面を自動起動したいとすると、シート上で”ワードアートの編集”と検索します。すると、該当のコマンドバーが見つかりますので、オブジェクトを選択した上でExecuteメソッドで実行します。 (ワードアート選択、テキスト編集画面表示サンプル)
Private Sub CommandButton1_Click() Dim Cmd As CommandBar Set Cmd = CommandBars("WordArt") ActiveSheet.Shapes("ワードアート 1").Select Cmd.Visible = True 'VisibleにしなくてもOK Cmd.Controls("テキストの編集(&X)...").Execute End Sub
※実際にはワードアートのダブルクリックで編集画面を開くことが出来ます。