How to have python wait until an Excel Macro/Refresh is done

自闭症网瘾萝莉.ら 提交于 2019-12-24 09:16:07

问题


I am using Python to run a macro in excel. and I want Python to close excel. The macro refreshes a data connection in excel which can be slow.

How do I have python wait until the refresh is done to close. This is what I am using, I need something before the xl.Quit that will wait until the refresh in macro is done????

 import win32com.client
 import os

 xl = win32com.client.DispatchEx("Excel.Application")
 wb = xl.workbooks.open("X:\Backoffice\Options Trading\BloombergRate.xlsm")
 xl.Visible = True
 xl.run("Refresh")
 xl.Quit()

Wait ways can I make this work?


回答1:


Edit your Refresh macro to set QueryTable.BackgroundQuery property to False. This should cause the macro to block until it is done.




回答2:


You don't even have to set up a macro to run this. win32com has a native method to refresh all data connections.

import win32com.client
import os

xl = win32com.client.DispatchEx("Excel.Application")
wb = xl.workbooks.open("X:\Backoffice\Options Trading\BloombergRate.xlsm")
xl.Visible = True
wb.RefreshAll()
xl.Quit()


来源:https://stackoverflow.com/questions/11832628/how-to-have-python-wait-until-an-excel-macro-refresh-is-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!