Home » ワードマクロ・Word VBAの使い方 » Office連携 » WordのCommandBarControlオブジェクト一覧をExcelに作成するマクロ

WordのCommandBarControlオブジェクト一覧をExcelに作成するマクロ

動作検証バージョン:Windows 11 Home + 64bit Word & Excel バージョン 2501(ビルド18429.20158クイック実行)

書式に関連するCommandBarControlオブジェクトの一覧を作成するExcelマクロをご紹介しています。

Wordの場合も気になったのでマクロを作ってみました。

[スポンサードリンク]

WordのCommandBarControlオブジェクト一覧をExcelに出力するサンプルマクロ

以下のWordマクロを実行すると、WordのCommandBarControlオブジェクト一覧が新規Excelブックに作成されます。

Sub 書式のCommandBarControlオブジェクトの一覧をExcelに作成する()

 Dim xl_app As Object, xl_bk As Object
 Set xl_app = CreateObject("Excel.Application")
 xl_app.Visible = True
 Set xl_bk = xl_app.Workbooks.Add

 Dim xl_sht As Object
 Set xl_sht = xl_bk.Sheets(1)

 xl_sht.Cells(1, "A").Value = "Index"
 xl_sht.Cells(1, "B").Value = "Caption"
 xl_sht.Cells(1, "C").Value = "ID"

 Dim n As Long: n = 2
 Dim itm As CommandBarControl
 For Each itm In Word.Application.CommandBars("Formatting").Controls
  xl_sht.Cells(n, "A").Value = itm.Index
  xl_sht.Cells(n, "B").Value = itm.Caption
  xl_sht.Cells(n, "C").Value = itm.ID
  n = n + 1
 Next

 xl_sht.Range("A1:C1").Font.Bold = True
 xl_sht.Columns("A:C").AutoFit

End Sub

基本的な考え方は、Excelマクロと同じです。
ただし、VBAのCreateObject関数を使ってExcel.Applicationオブジェクトの作成等を行う処理を行っていること、

 Dim xl_app As Object, xl_bk As Object
 Set xl_app = CreateObject("Excel.Application")
 xl_app.Visible = True
 Set xl_bk = xl_app.Workbooks.Add

出力先Excelのシートに関するコードは、変数xl_shtを経由して行っていることあたりが

 Dim xl_sht As Object
 Set xl_sht = xl_bk.Sheets(1)

Excel VBA版とは異なります。

[スポンサードリンク]

Home » ワードマクロ・Word VBAの使い方 » Office連携 » WordのCommandBarControlオブジェクト一覧をExcelに作成するマクロ

「Office連携」の記事一覧

検索


Copyright © インストラクターのネタ帳 All Rights Reserved.

.