pythoncom error invoking COM method excel new sheet

2.2k views Asked by At

I have been working on some Python/Excel application, however I ran into an interesting issue. I have simplified the example to the extent producing the error.

(Warning - infinite loop without break mechanism)

from win32com.client import DispatchWithEvents
import msvcrt, pythoncom
import time


def TestExcel():
    class ExcelEvents:
        pass

    class WorkbookEvents:
        pass

    e = DispatchWithEvents("Excel.Application", ExcelEvents)
    e.Visible = 1
    book = e.Workbooks.Add()
    book = DispatchWithEvents(book, WorkbookEvents)

    while True:
        pythoncom.PumpWaitingMessages()
        time.sleep(0.1)


if __name__ == '__main__':
    TestExcel()
    print('Exiting')

When ran it opens Excel, adds new workbook and then waits for event either on application or workbook object.

If I do any possible events in Excel, nothing happens (as there are no handlers defined in event classes), only when adding new sheet to the workbook. The following error is produced immediately:

pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable
pythoncom error: Python error invoking COM method.

Traceback (most recent call last):
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 278, in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 283, in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 650, in _invokeex_
    return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)
  File "C:\Users\majkop\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\server\policy.py", line 586, in _invokeex_
    return func(*args)
TypeError: 'NoneType' object is not callable

I know I can put try/except around pythoncom.PumpWaitingMessage() and I know it's the pythonic way :D however I would still be interested in knowing what causes this error... 4 times in row


EDIT: try/except around pump doesn't work to avoid display of the error

0

There are 0 answers