{"id":2060,"date":"2026-05-13T16:02:23","date_gmt":"2026-05-13T08:02:23","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2060"},"modified":"2026-03-28T05:56:53","modified_gmt":"2026-03-28T05:56:53","slug":"how-to-use-applicationcommandunderlines-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationcommandunderlines-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.CommandUnderlines in the xlwings API way"},"content":{"rendered":"\n<p>The <strong>Application.CommandUnderlines<\/strong> property in Excel VBA controls the underline style used for menu command access keys (the underlined letter that, when pressed with the Alt key, activates a command). In the xlwings Python library, which provides a programmatic interface to Excel&#8217;s object model, you can access and manipulate this property to adjust the user interface behavior of the Excel application instance. This can be useful for ensuring consistency in application appearance or for automating UI configuration tasks in scripts that interact with Excel via xlwings.<\/p>\n\n\n\n<p><strong>Functionality<\/strong><br>The <code>CommandUnderlines<\/code> property determines whether access key underlines in Excel menus and dialog boxes are always visible, visible only when the Alt key is pressed, or follow the system setting. This is a remnant of older UI conventions but can still be relevant for accessibility or specific user preference scenarios when automating Excel. In xlwings, you can both read the current setting and change it programmatically.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><br>In xlwings, you access this property through the <code>app<\/code> object, which represents the Excel Application. The property is exposed as a simple attribute.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.CommandUnderlines<\/code><\/pre>\n\n\n\n<p>This property accepts and returns an integer value corresponding to the <code>XlCommandUnderlines<\/code> enumeration. The primary values are:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Value<\/th><th>Constant (VBA)<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td><code>xlCommandUnderlinesAutomatic<\/code><\/td><td>Underlines appear based on the system setting.<\/td><\/tr><tr><td>1<\/td><td><code>xlCommandUnderlinesOff<\/code><\/td><td>Underlines are never shown.<\/td><\/tr><tr><td>2<\/td><td><code>xlCommandUnderlinesOn<\/code><\/td><td>Underlines are always shown.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Code Examples<\/strong><br>Here are practical examples using xlwings to work with the <code>CommandUnderlines<\/code> property.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reading the Current Setting:<\/strong><br>This code retrieves the current underline setting and prints a descriptive message.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance or start a new one\napp = xw.apps.active\n\n# Get the current CommandUnderlines setting\ncurrent_setting = app.api.CommandUnderlines\n\n# Map the integer value to a description\nsetting_map = {\n0: \"Automatic (follows system)\",\n1: \"Off (never shown)\",\n2: \"On (always shown)\"\n}\ndescription = setting_map.get(current_setting, \"Unknown setting\")\nprint(f\"Current CommandUnderlines setting: {current_setting} ({description})\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Changing the Setting:<\/strong><br>This script changes the setting to always show underlines.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\napp = xw.apps.active\n\n# Set CommandUnderlines to always show (xlCommandUnderlinesOn)\napp.api.CommandUnderlines = 2 # You can also use the constant 2 directly\n\nprint(\"Command underlines are now set to be always visible.\")<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Toggling the Setting Based on Current State:<\/strong><br>A more advanced example that toggles the setting between &#8220;On&#8221; and &#8220;Off&#8221;.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\napp = xw.apps.active\n\ncurrent = app.api.CommandUnderlines\nif current == 2: # If currently On\n    new_setting = 1 # Turn Off\n    print(\"Toggling command underlines OFF.\")\nelse:\n    new_setting = 2 # Otherwise, turn On\n    print(\"Toggling command underlines ON.\")\n\napp.api.CommandUnderlines = new_setting<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **Application.CommandUnderlines** property in Excel VBA controls the underline style used for me&#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-2060","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2060","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=2060"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2060\/revisions"}],"predecessor-version":[{"id":3153,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2060\/revisions\/3153"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}