{"id":2006,"date":"2026-04-16T16:14:10","date_gmt":"2026-04-16T08:14:10","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2006"},"modified":"2026-03-28T04:44:15","modified_gmt":"2026-03-28T04:44:15","slug":"how-to-use-applicationontime-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationontime-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.OnTime in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The OnTime method in Excel&#8217;s Application object is a powerful feature for scheduling the execution of a procedure at a specific future time or after a specific time interval. In xlwings, this functionality is accessed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This allows for the automation of repetitive tasks, data refreshes, or timed notifications without user intervention, effectively enabling time-driven macros within a Python-controlled Excel environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary function is to run a specified VBA macro (procedure) at a predetermined time. It can be used for one-time execution or to set up recurring schedules. While xlwings itself runs Python code, <code>OnTime<\/code> schedules the execution of VBA macros stored in the Excel workbook. Therefore, typical use involves writing a VBA macro that, when triggered, can call back into Python via xlwings&#8217; <code>RunPython<\/code> function or perform native Excel operations. This creates a hybrid automation model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>The call is made through the Excel Application object. The syntax in xlwings is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.OnTime(EarliestTime, Procedure, LatestTime, Schedule)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>EarliestTime (Required):<\/strong> The time when the procedure should be run. It is a serial Excel date\/time value. In practice, it&#8217;s often created using <code>datetime<\/code> or <code>time<\/code> modules. Example: <code>datetime.datetime.now() + datetime.timedelta(seconds=10)<\/code>.<\/li>\n\n\n\n<li><strong>Procedure (Required):<\/strong> A string specifying the name of the VBA macro to run. This macro must be present in a standard VBA module in the workbook (e.g., &#8220;Module1.MyMacro&#8221;).<\/li>\n\n\n\n<li><strong>LatestTime (Optional):<\/strong> The latest time for the procedure to run. If Excel is not in Ready, Copy, Cut, or Find mode at <code>EarliestTime<\/code>, it will wait until it enters one of these states, but only until <code>LatestTime<\/code>. If omitted, Excel waits indefinitely.<\/li>\n\n\n\n<li><strong>Schedule (Optional):<\/strong> A boolean value. <code>True<\/code> to schedule a new <code>OnTime<\/code> procedure (default). <code>False<\/code> to clear a previously set procedure that has not yet run.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Example:<\/strong><br>This example schedules a VBA macro named &#8220;RefreshData&#8221; to run 5 seconds from now. The VBA macro itself could contain code to call a Python function or refresh queries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nimport datetime\n\n# Connect to the active Excel instance or create a new one\napp = xw.apps.active\n\n# Calculate the time for execution (5 seconds from now)\nrun_time = datetime.datetime.now() + datetime.timedelta(seconds=5)\n\n# Schedule the OnTime call. The macro \"RefreshData\" must exist in the workbook.\napp.api.OnTime(EarliestTime=run_time, Procedure=\"RefreshData\")\n\nprint(f\"Scheduled 'RefreshData' to run at approximately {run_time}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To cancel a scheduled procedure before it executes, you would call <code>OnTime<\/code> with the same <code>EarliestTime<\/code> and <code>Procedure<\/code>, but set <code>Schedule<\/code> to <code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Cancel the previously scheduled \"RefreshData\" macro\napp.api.OnTime(EarliestTime=run_time, Procedure=\"RefreshData\", Schedule=False)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The OnTime method in Excel&apos;s Application object is a powerful feature for scheduling the execution o&#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-2006","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2006","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=2006"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2006\/revisions"}],"predecessor-version":[{"id":3059,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2006\/revisions\/3059"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}