Error while working with excel using python

sagar picture sagar · Sep 15, 2010 · Viewed 9.7k times · Source

while my script is updating one excel same time if i am going to do any other work manually with another excel error occurs i am using dispatch

     from win32com.client import Dispatch

     excel    = Dispatch('Excel.Application')
excel.Visible   = True 

file_name="file_name.xls"
workbook = excel.Workbooks.Open(file_name)
workBook  = excel.ActiveWorkbook
sheet=workbook.Sheets(sheetno)

I am geting error like this (, com_error(-2147418111, 'Call was rejected by callee.', None, None)

Is there is any way to overcome it ..can i update another excel without geting error..

Answer

McG picture McG · Jul 21, 2017

I encountered this same issue recently. While it sounds like there can be multiple root causes, my situation was occurring because Python was making subsequent calls too quickly for Excel to keep up, particularly with external query refreshes. I resolved this intermittent "Call was rejected by callee" error by inserting time.sleep() between most of my calls and increasing the sleep argument for any calls that are particularly lengthy (usually between 7-15 seconds). This allows Excel the time to complete each command before Python issued additional commands.