完全な備忘録
マクロにいちいちショートカット設定するのめんどくさいし
重要なファイル触ってる時に間違って起動しちゃうのこわいし
ってことでアドイン使ってリボンから起動できるようにする
探せば先駆者がいると思うけどあとから柔軟に追加とかできるようにしてあるから そこはまあいい点な気がする
まずはマクロ
テストマクロ.xlsm
Sub No1_テストマクロ()
' 処理内容
' 今回は適当に
MsgBox "うんち"
End Sub
Sub No2_テストマクロ()
' 処理内容
MsgBox "ちんちn"
End Sub
アドイン
標準モジュールを追加
テストマクロアドイン.xlam
Public Sub テストマクロアドイン(control As IRibbonControl)
Dim id
id = control.id
Call Application.Run("テストマクロ.xlsm!" & id)
End Sub
アドインファイルを分解しxmlを編集
こちらのサイトが画像付きでわかりやすい
https://resthill.blog/addin-design1/
アドインファイルの拡張子を手動でzipに変換
マクロアドイン.xlam => マクロアドイン.zip
↓ 警告出るけど無視
↓ 展開するとこんな感じ
↓展開後、カスタム用のフォルダ&カスタム用xmlを作成
customUI.xml・その他設定
Point
id = テストマクロのマクロ名
onAction = テストマクロアドイン
customUI.xml
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
<tabs>
<tab id="このidはなんでもいいよ" label="リボンに表示されちゃう">
<group id="おてぃnてぃn" label="ここもリボンに表示さr">
<button id="No1_テストマクロ" label="No1" size="large" onAction="テストマクロアドイン"/>
<button id="No2_テストマクロ" label="No2" size="large" onAction="テストマクロアドイン"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
_relsフォルダ内、.relsファイルを編集
Relationshipタグが並んでいるところに最後以下を追記
追記文
<Relationship Id="majinanndemoii" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml"/>
.rels(追記後)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
<Relationship Id="majinanndemoii" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml"/>
</Relationships>
あとは展開したファイルを再度圧縮(zip)→拡張子変更(xlam)
テストマクロ起動&アドインファイルを起動してリボンが追加され、ボタンで動くようになるはず
以上!