{"id":2155,"date":"2026-06-30T07:37:49","date_gmt":"2026-06-29T23:37:49","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2155"},"modified":"2026-03-28T09:08:43","modified_gmt":"2026-03-28T09:08:43","slug":"how-to-use-applicationonwindow-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationonwindow-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.OnWindow in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Excel&#8217;s object model, the <code>Application.OnWindow<\/code> property is a very useful member that allows developers to assign a macro or a procedure to run whenever any workbook window is activated (i.e., brought to the front) within the Excel application. This enables automation of tasks that should respond to window switching, such as updating a dashboard, refreshing data, or adjusting UI elements based on the active workbook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, which provides a Pythonic way to interact with Excel via its COM API, you can access this property through the <code>Application<\/code> object. The <code>OnWindow<\/code> property is a read\/write string that accepts the name of a macro (as stored in Excel) to be executed. It is important to note that the assigned macro must be available in a currently open workbook, typically within a standard module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Parameters:<\/strong><br>In xlwings, you can set or get the <code>OnWindow<\/code> property using the following approach:<\/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# Set the OnWindow property to a macro name\napp.api.OnWindow = \"MacroName\"\n\n# Get the current OnWindow property value\ncurrent_macro = app.api.OnWindow<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>app.api.OnWindow<\/code><\/strong>: This accesses the underlying COM <code>Application.OnWindow<\/code> property. The <code>api<\/code> attribute in xlwings provides direct access to the raw Excel object model.<\/li>\n\n\n\n<li><strong>Value<\/strong>: The property expects a string that is the name of a macro (e.g., <code>\"MyWindowHandler\"<\/code>). To clear the assignment, set it to an empty string (<code>\"\"<\/code>).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The macro specified must be written in VBA and reside in a module of an open workbook. It cannot be a Python function directly; xlwings can bridge this by calling Python from VBA, but the <code>OnWindow<\/code> property itself only accepts VBA macro names.<\/li>\n\n\n\n<li>The event triggers whenever any workbook window is activated, including switching between different windows of the same workbook.<\/li>\n\n\n\n<li>This property is application-wide, meaning it affects all workbooks open in that Excel instance.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Usage:<\/strong><br>Suppose you have a VBA macro named <code>UpdateStatusBar<\/code> in a workbook that updates the status bar with the active window&#8217;s name. You can assign it via xlwings as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Start or connect to Excel\napp = xw.App(visible=True)\n\n# Open a workbook containing the macro (e.g., 'Book1.xlsm')\nwb = app.books.open('Book1.xlsm')\n\n# Set the OnWindow property to trigger the macro\napp.api.OnWindow = \"UpdateStatusBar\"\n\n# Now, whenever you switch windows, the macro will run\n# For demonstration, activate another window\napp.books.open('Book2.xlsx').activate()\n\n# To check the current assignment\nprint(f\"OnWindow macro is set to: {app.api.OnWindow}\")\n\n# To remove the assignment\napp.api.OnWindow = \"\"\n\n# Close the workbooks and quit\nwb.close()\napp.quit()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Excel&apos;s object model, the `Application.OnWindow` property is a very useful member that allows dev&#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-2155","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2155","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=2155"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2155\/revisions"}],"predecessor-version":[{"id":3295,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2155\/revisions\/3295"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}