今回はボタンの画像(組込アイコン)を切り替える方法を紹介します。
1. Officeファイルを開きます(今回はExcelファイル)。
2. 標準モジュールに下記コードを貼り付けて上書き保存した後、ファイルを閉じます。
Option Explicit
Private myRibbon As Office.IRibbonUI
Private btnID(0 To 2) As String
Private n As Long
Public Sub Ribbon_onLoad(ribbon As IRibbonUI)
Set myRibbon = ribbon
n = 0 '初期化
SetControlID
End Sub
Public Sub button_onAction(control As IRibbonControl)
Select Case control.ID
Case "btnChangeImageMso"
n = n + 1
If n > 2 Then n = 0
myRibbon.InvalidateControl control.ID
End Select
End Sub
Public Sub button_getImage(control As IRibbonControl, ByRef returnedVal)
Select Case control.ID
Case "btnChangeImageMso"
returnedVal = btnID(n)
End Select
End Sub
Private Sub SetControlID()
'コントロールIDセット
btnID(0) = "SadFace"
btnID(1) = "HappyFace"
btnID(2) = "Heart"
End Sub
<?xml version="1.0" encoding="utf-8"?>
<customUI onLoad="Ribbon_onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="tabChangeButtonImageMso" label="Change ButtonImage" visible="true">
<group id="grpChangeButtonImageMso" label="Change ButtonImage">
<button id="btnChangeImageMso" label="Change Image" size="large" getImage="button_getImage" onAction="button_onAction" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>