HPのお問い合わせフォームから下記の質問がありました。
「メニューの内容を動的に変更する」にあるサンプル画像を見たところ、ドロップダウンリストのようになっていますが、これらは1つ1つに別の動作を指示したVBAを登録できるようにできますでしょうか?
dynamicMenu要素のgetContent属性を使って動的にメニューを読み込み、その子要素から任意のマクロを実行したい、ということだと思いますが、これは子要素のonAction属性で対応することができます。
リボンXML
<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab id="tabSample" label="Sample Tab">
<group id="grpSample" label="Sample Group">
<dynamicMenu id="dmuSample" label="Sample Menu" imageMso="HappyFace" size="large" getContent="dmuSample_getContent" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
標準モジュール
Option Explicit
Public Sub dmuSample_getContent(control As IRibbonControl, ByRef returnedVal)
returnedVal = GetContents()
End Sub
Public Sub btnSample1_onAction(control As IRibbonControl)
'[btnSample1]から呼び出すプロシージャ
MsgBox "【" & control.ID & "】をクリックしました。", vbInformation + vbSystemModal
End Sub
Public Sub btnSample2_onAction(control As IRibbonControl)
'[btnSample2]から呼び出すプロシージャ
MsgBox "[" & control.ID & "]をクリックしました。", vbExclamation + vbSystemModal
End Sub
Public Sub btnSample_onAction(control As IRibbonControl)
Select Case control.ID 'id属性の値で処理分岐
Case "btnSample3"
MsgBox "[" & control.ID & "]をクリックしましたね?", vbQuestion + vbSystemModal
Case "btnSample4"
MsgBox "あなたがクリックしたのは【" & control.ID & "】です。", vbCritical + vbSystemModal
End Select
End Sub
Private Function GetContents() As String
Dim d As Object
Dim elmMenu As Object
Dim elmButton As Object
Set d = CreateObject("Msxml2.DOMDocument")
Set elmMenu = d.createElement("menu")
elmMenu.setAttribute "xmlns", "http://schemas.microsoft.com/office/2006/01/customui"
elmMenu.setAttribute "itemSize", "large"
Set elmButton = d.createElement("button")
With elmButton
.setAttribute "id", "btnSample1"
.setAttribute "label", "Button1"
.setAttribute "imageMso", "A"
.setAttribute "onAction", "btnSample1_onAction" '実行するプロシージャ(固有)を指定
End With
elmMenu.appendChild elmButton
Set elmButton = Nothing
Set elmButton = d.createElement("button")
With elmButton
.setAttribute "id", "btnSample2"
.setAttribute "label", "Button2"
.setAttribute "imageMso", "B"
.setAttribute "onAction", "btnSample2_onAction" '実行するプロシージャ(固有)を指定
End With
elmMenu.appendChild elmButton
Set elmButton = Nothing
Set elmButton = d.createElement("button")
With elmButton
.setAttribute "id", "btnSample3"
.setAttribute "label", "Button3"
.setAttribute "imageMso", "C"
.setAttribute "onAction", "btnSample_onAction" '実行するプロシージャ(共通)を指定
End With
elmMenu.appendChild elmButton
Set elmButton = Nothing
Set elmButton = d.createElement("button")
With elmButton
.setAttribute "id", "btnSample4"
.setAttribute "label", "Button4"
.setAttribute "imageMso", "D"
.setAttribute "onAction", "btnSample_onAction" '実行するプロシージャ(共通)を指定
End With
elmMenu.appendChild elmButton
Set elmButton = Nothing
d.appendChild elmMenu
GetContents = d.XML
End Function
上記コードを設定したファイルを開くと、下図のように「Sample Menu」からボタンを選択することで、任意のマクロを呼び出せることが確認できます。
上記標準モジュールのコードの通り、それぞれのボタン毎に呼び出すマクロを設定することもできますし(btnSample1,btnSample2)、共通のマクロを呼び出すようにしてid属性の値(control.ID)で処理を分けることもできます(btnSample3,btnSample4)。



















この記事へのコメントはありません。