動作検証バージョン:Windows 11 Home + 64bit Excel & Word バージョン 2501(ビルド18429.20132クイック実行)
ExcelのCommandBarオブジェクト一覧を作成するExcelマクロ、WordのCommandBarオブジェクト一覧を作成するExcelマクロの出力結果を見ると、同じIndexでもまったく別のCommandBarであることがわかります。
実はIndexプロパティとは別に、非表示になっているIdプロパティが用意されています。
ExcelとWordのCommandBarオブジェクトの
Index
Id
Name
NameLocal
プロパティを出力するマクロを作ってみました。
[スポンサードリンク]
ExcelとWordのCommandBarオブジェクト一覧を作成するサンプルマクロ
以下のExcelマクロを実行すると、アクティブブックの先頭にシートが新規に挿入されて、ExcelとWordのCommandBarオブジェクト一覧が作成されます。
Sub ExcelとWordのCommandBarオブジェクト一覧を作成する()
Sheets.Add Before:=Sheets(1)
Cells(1, "A").Value = "Excel" & vbLf & "Index"
Cells(1, "B").Value = "Excel" & vbLf & "Id"
Cells(1, "C").Value = "Excel" & vbLf & "Name"
Cells(1, "D").Value = "Excel" & vbLf & "NameLocal"
Cells(1, "E").Value = "Word" & vbLf & "Index"
Cells(1, "F").Value = "Word" & vbLf & "Id"
Cells(1, "G").Value = "Word" & vbLf & "Name"
Cells(1, "H").Value = "Word" & vbLf & "NameLocal"
Dim i As Long
With Application.CommandBars
For i = 1 To .Count
Cells(i + 1, "A").Value = .Item(i).Index
Cells(i + 1, "B").Value = .Item(i).ID
Cells(i + 1, "C").Value = .Item(i).Name
Cells(i + 1, "D").Value = .Item(i).NameLocal
Next
End With
With CreateObject("Word.Application").CommandBars
For i = 1 To .Count
Cells(i + 1, "E").Value = .Item(i).Index
Cells(i + 1, "F").Value = .Item(i).ID
Cells(i + 1, "G").Value = .Item(i).Name
Cells(i + 1, "H").Value = .Item(i).NameLocal
Next
End With
Range("A1:H1").Font.Bold = True
Columns("A:H").AutoFit
End Sub
Sheets.Add Before:=Sheets(1)
Cells(1, "A").Value = "Excel" & vbLf & "Index"
Cells(1, "B").Value = "Excel" & vbLf & "Id"
Cells(1, "C").Value = "Excel" & vbLf & "Name"
Cells(1, "D").Value = "Excel" & vbLf & "NameLocal"
Cells(1, "E").Value = "Word" & vbLf & "Index"
Cells(1, "F").Value = "Word" & vbLf & "Id"
Cells(1, "G").Value = "Word" & vbLf & "Name"
Cells(1, "H").Value = "Word" & vbLf & "NameLocal"
Dim i As Long
With Application.CommandBars
For i = 1 To .Count
Cells(i + 1, "A").Value = .Item(i).Index
Cells(i + 1, "B").Value = .Item(i).ID
Cells(i + 1, "C").Value = .Item(i).Name
Cells(i + 1, "D").Value = .Item(i).NameLocal
Next
End With
With CreateObject("Word.Application").CommandBars
For i = 1 To .Count
Cells(i + 1, "E").Value = .Item(i).Index
Cells(i + 1, "F").Value = .Item(i).ID
Cells(i + 1, "G").Value = .Item(i).Name
Cells(i + 1, "H").Value = .Item(i).NameLocal
Next
End With
Range("A1:H1").Font.Bold = True
Columns("A:H").AutoFit
End Sub
この出力結果を見ると、CommandBar.Idプロパティの値が、ExcelとWordで基本的には同じであることがわかります。
[スポンサードリンク]
Home » エクセルマクロ・Excel VBAの使い方 » Office連携 » ExcelとWordのCommandBarオブジェクト一覧を作成するマクロ