The CommandBars Collection

The topmost object that relates to menus and toolbars is the CommandBars collection, which contains all of the application’s CommandBar objects. The CommandBars collection is accessible through the CommandBars property of the Application object; that is

	Application.CommandBars

(CommandBars is also a global property, so it can be used without the qualifying word “Application.”) The following code will print a list of all of the CommandBar objects to the Immediate window. I think you will be surprised at the large number of these objects, most of which are not currently visible:

	Dim sType as string, cbar as CommandBar
	For Each cbar In CommandBars
	  Select Case cbar.Type
	  Case msoBarTypeNormal
	     sType = "Normal"
	  Case msoBarTypeMenuBar
	     sType = "Menu bar"
	  Case msoBarTypePopup
	     sType = "Popup"
	  End Select
	  Debug.Print cbar.Name & "," & sType & "," & cbar.Visible
	Next

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.