Office関連

[リボン・カスタマイズ]dynamicMenu要素から任意のマクロを実行する。

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」からボタンを選択することで、任意のマクロを呼び出せることが確認できます。

dynamicMenu_Sample_01

上記標準モジュールのコードの通り、それぞれのボタン毎に呼び出すマクロを設定することもできますし(btnSample1,btnSample2)、共通のマクロを呼び出すようにしてid属性の値(control.ID)で処理を分けることもできます(btnSample3,btnSample4)。

SeleniumBasic(Selenium VBA)がMicrosoft Edgeに対応しました。前のページ

[Windows]「AppsFolder」のような特殊フォルダ名を取得する。次のページ

関連記事

  1. アイコン一覧

    Office 365アイコン(imageMso)一覧(E)

    Office 365のデスクトップ版Officeアプリケーション(Wo…

  2. Office関連

    Office XP Developer Toolsでリボン対応のCOMアドインを作成する。

    「Visual Basic 6でリボン対応のアドインを作成する」ではV…

  3. Office関連

    コマンドマクロ一覧(Word 2013 Customer Preview)

    Word 2013 CP版に組み込まれている「コマンドマクロ」のコマン…

  4. アイコン一覧

    Office 2013 アイコン一覧(E)

    ・Office 2013 アイコン一覧 NUM…

  5. Office関連

    Microsoft Edgeを操作するVBAマクロ(WebDriver編)

    Microsoft Edge Dev Blogに「Bringing a…

  6. Office関連

    ConvertToTextメソッドを使ってテーブルを二次元配列に変換するWordマクロ

    WordのTableオブジェクトには、テーブルを解除して文字列に変換す…

コメント

  • コメント (0)

  • トラックバックは利用できません。

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

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP