{"id":2059,"date":"2026-05-13T07:34:40","date_gmt":"2026-05-12T23:34:40","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2059"},"modified":"2026-03-28T05:56:12","modified_gmt":"2026-03-28T05:56:12","slug":"how-to-use-applicationcommandbars-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationcommandbars-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.CommandBars in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>CommandBars<\/code> member of the <code>Application<\/code> object in Excel&#8217;s object model represents the collection of all command bars, which include toolbars and menus, in the application. In modern Excel, command bars are largely superseded by the Ribbon interface, but they remain accessible for compatibility and custom UI development. Using xlwings, you can interact with <code>CommandBars<\/code> to customize or retrieve information about these UI elements programmatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The xlwings API provides a way to access the <code>CommandBars<\/code> collection through the <code>Application<\/code> object. The syntax for accessing it is straightforward: <code>app.api.CommandBars<\/code>. Here, <code>app<\/code> is an instance of the xlwings <code>App<\/code> class, which represents the Excel application. The <code>.api<\/code> property exposes the underlying COM object, allowing direct use of Excel&#8217;s VBA object model members. The <code>CommandBars<\/code> object itself is a collection, and you can reference specific command bars by name or index. For example, <code>app.api.CommandBars(\"Standard\")<\/code> refers to the Standard toolbar. Key methods and properties include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Count<\/code>: Returns the number of command bars.<\/li>\n\n\n\n<li><code>Item(index)<\/code>: Retrieves a specific <code>CommandBar<\/code> object by index or name.<\/li>\n\n\n\n<li><code>Add(Name, Position, MenuBar, Temporary)<\/code>: Creates a new custom command bar. Parameters: <code>Name<\/code> (string, the bar&#8217;s name), <code>Position<\/code> (integer, e.g., <code>1<\/code> for top, <code>2<\/code> for left), <code>MenuBar<\/code> (boolean, whether it&#8217;s a menu bar), <code>Temporary<\/code> (boolean, deleted on Excel exit).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To use these, you can call methods directly on <code>app.api.CommandBars<\/code>. For instance, to add a custom toolbar, you might specify <code>Position<\/code> as <code>1<\/code> (msoBarTop) from the <code>MsoBarPosition<\/code> enumeration. Note that xlwings does not have built-in constants for these enumerations; you may need to define them or use their integer values based on Microsoft documentation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a code example demonstrating the use of <code>CommandBars<\/code> with xlwings. This script lists all command bars and creates a custom one, adding a button to run a simple macro.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance\napp = xw.apps.active\n\n# Access the CommandBars collection\ncommand_bars = app.api.CommandBars\n\n# Print the count and names of all command bars\nprint(f\"Total command bars: {command_bars.Count}\")\nfor i in range(1, command_bars.Count + 1):\n    cb = command_bars.Item(i)\n    print(f\" {i}: {cb.Name}\")\n\n# Define constants for MsoBarPosition (from Microsoft documentation)\nmsoBarTop = 1\nmsoBarLeft = 2\nmsoBarRight = 3\nmsoBarBottom = 4\nmsoBarFloating = 5\nmsoBarPopup = 6\n\n# Add a custom command bar (toolbar)\ncustom_bar = command_bars.Add(Name=\"MyCustomBar\", Position=msoBarTop, MenuBar=False, Temporary=True)\nprint(f\"Created custom bar: {custom_bar.Name}\")\n\n# Add a button to the custom bar (using CommandBarControls)\n# Note: This requires further setup with OnAction to link to a macro\nbutton = custom_bar.Controls.Add(Type=1) # Type 1 is msoControlButton\nbutton.Caption = \"Run Macro\"\nbutton.TooltipText = \"Click to execute a macro\"\n\n# Make the custom bar visible\ncustom_bar.Visible = True\n\n# Clean up: Delete the custom bar (optional, since Temporary=True will remove it on exit)\n# custom_bar.Delete()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `CommandBars` member of the `Application` object in Excel&apos;s object model represents the collecti&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2059","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2059"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2059\/revisions"}],"predecessor-version":[{"id":3151,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2059\/revisions\/3151"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}