Issue in using win32com to access Excel file
Renaming the GenPy
folder should work.
It's present at: C:\Users\ _insert_username_ \AppData\Local\Temp\gen_py
Renaming it will create a new Gen_py
folder and will let you dispatch Excel properly.
The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).
- In Late Binding, whenever a method is called, the object is queried for the method and if it succeeds, then the call can be made.
- In Early Binding, the information of the object model is determined in advance from type information supplied by the object call. Early binding makes use of MakePy. Also, early binding is case sensitive.
There are two ways to fix this issue:
Use the dynamic module to force your code to work in a late-bound oriented way. Example use:
"win32com.client.dynamic.Dispatch()" instead of "win32com.client.Dispatch()"
Use camelcase sensitive keywords for the early bound oriented way. Example use:
"excel.Visible()" instead of "excel.VISIBLE()" or "excel.visible()"
Try out
"win32com.client.dynamic.Dispatch()" instead of "win32com.client.gencache.EnsureDispatch"
As win32com.client.gencache.EnsureDispatch forces the MakePy process.
A solution is to locate the gen_py folder (C:\Users\\AppData\Local\Temp\gen_py) and delete its content. It works for me when using the COM with another program.