「外部のXMLファイルを読み込み、ユーザー名に応じてmenu内容を変更する」の関連で、dynamicMenu要素のgetContent属性のコールバックを利用してmenu内にあるbuttonの数を増やす方法を紹介します。
[リボンXML]
<?xml version="1.0" encoding="utf-8"?>
<customUI onLoad="rbnSample_onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab id="tabSample" label="Sample Tab">
<group id="grpSample" label="Sample Group">
<button id="btnSample" label="Button増加" size="large" imageMso="PlusSign" onAction="btnSample_onAction" />
<dynamicMenu id="dmuSample" label="Sample Menu" size="large" imageMso="HappyFace" getContent="dmuSample_getContent" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
[標準モジュール]
Option Explicit
Private myRibbon As Office.IRibbonUI
Private Sub rbnSample_onLoad(ribbon As IRibbonUI)
Set myRibbon = ribbon
End Sub
Private Sub btnSample_onAction(control As IRibbonControl)
IncButtonElement ThisWorkbook.Path & "\Sample.xml", "btnSample", "Sample Button"
myRibbon.InvalidateControl "dmuSample"
End Sub
Private Sub dmuSample_getContent(control As IRibbonControl, ByRef returnedVal)
Dim MenuXML As String
MenuXML = LoadMenuFile(ThisWorkbook.Path & "\Sample.xml")
If Len(Trim$(MenuXML)) < 1 Then Exit Sub
returnedVal = MenuXML
End Sub
Private Function LoadMenuFile(ByVal FilePath As String) As String
Dim ret As String
ret = "" '初期化
On Error Resume Next
With CreateObject("Msxml2.DOMDocument")
.async = False
If .Load(FilePath) Then ret = .XML
End With
On Error GoTo 0
LoadMenuFile = ret
End Function
Private Sub IncButtonElement(ByVal XmlFilePath As String, ByVal BtnID As String, ByVal BtnLabel As String)
'button要素増加
Dim d As Object
Dim n As Object
Dim cnt As Long
Set d = CreateObject("Msxml2.DOMDocument")
d.async = False
If d.Load(XmlFilePath) Then
cnt = d.getElementsByTagName("button").Length
'[xmlns=""]が追加されるのを防ぐために名前空間URI設定
Set n = d.createNode(1, "button", d.DocumentElement.NamespaceURI)
With d.DocumentElement.appendChild(n)
.setAttribute "id", BtnID & cnt + 1
.setAttribute "label", BtnLabel & cnt + 1
.setAttribute "imageMso", "HappyFace"
End With
Set n = Nothing
d.Save XmlFilePath
End If
Set d = Nothing
End Sub
上記コードを設定したファイルと同じ場所に、下記XMLファイルを保存します。
[Sample.xml]
<menu xmlns="http://schemas.microsoft.com/office/2006/01/customui" itemSize="large"> <button id="btnSample1" label="Sample Button1" imageMso="HappyFace"/> </menu>
XMLファイル保存後、リボン・カスタマイズしたファイルを開くと「Sample Tab」タブが下図のように表示されます。

「Button増加」ボタンをクリックすることで、「Sample Menu」内にあるbuttonの数を増やすことができます。

上記のように、menu内容を外部のXMLファイルから読み込むようにして、マクロでXMLファイルを書き換えた後に再度XMLファイルを読み込むようにすれば、動的にリボン(の一部)を変更することができます。















増やしたのはdynamicMenuの子であって
Menuの子じゃないですよね。
splitButton内のmenuの子を増やす方法を探していたので
タイトルと違う内容だったのが残念です
test様
当ブログ管理者のきぬあさです。
dynamicMenu要素を使ってsplitButton要素の子要素を動的に変更する一例について記事を書きましたので、よろしければご参照いただければと思います。
・[リボン・カスタマイズ]splitButton要素の内容を動的に変更する。
http://www.ka-net.org/blog/?p=5081