The Application.CommandUnderlines 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’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.
Functionality
The CommandUnderlines 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.
Syntax
In xlwings, you access this property through the app object, which represents the Excel Application. The property is exposed as a simple attribute.
app.api.CommandUnderlines
This property accepts and returns an integer value corresponding to the XlCommandUnderlines enumeration. The primary values are:
| Value | Constant (VBA) | Description |
|---|---|---|
| 0 | xlCommandUnderlinesAutomatic | Underlines appear based on the system setting. |
| 1 | xlCommandUnderlinesOff | Underlines are never shown. |
| 2 | xlCommandUnderlinesOn | Underlines are always shown. |
Code Examples
Here are practical examples using xlwings to work with the CommandUnderlines property.
- Reading the Current Setting:
This code retrieves the current underline setting and prints a descriptive message.
import xlwings as xw
# Connect to the active Excel instance or start a new one
app = xw.apps.active
# Get the current CommandUnderlines setting
current_setting = app.api.CommandUnderlines
# Map the integer value to a description
setting_map = {
0: "Automatic (follows system)",
1: "Off (never shown)",
2: "On (always shown)"
}
description = setting_map.get(current_setting, "Unknown setting")
print(f"Current CommandUnderlines setting: {current_setting} ({description})")
- Changing the Setting:
This script changes the setting to always show underlines.
import xlwings as xw
app = xw.apps.active
# Set CommandUnderlines to always show (xlCommandUnderlinesOn)
app.api.CommandUnderlines = 2 # You can also use the constant 2 directly
print("Command underlines are now set to be always visible.")
- Toggling the Setting Based on Current State:
A more advanced example that toggles the setting between “On” and “Off”.
import xlwings as xw
app = xw.apps.active
current = app.api.CommandUnderlines
if current == 2: # If currently On
new_setting = 1 # Turn Off
print("Toggling command underlines OFF.")
else:
new_setting = 2 # Otherwise, turn On
print("Toggling command underlines ON.")
app.api.CommandUnderlines = new_setting
Leave a Reply