Search code examples
pythonexcelpyinstallerpywin32sap-gui

Pyinstaller exe error "win32com\client\dynamic.py", line 543, in __getattr__ pywintypes.com_error: (-2147221168, 'Could not read key from registry'


I have converted the .py to .exe file using pyinstaller to distribute to my users Hence my users do not have Python installed.

I used win32com to open excel & run macro in excel which will also open SAP

In my laptop I have no issue but when my user run the exe file, an error briefly pop up in console then it immediately closed. I had to run in cmd to see the error below

  File "AutoTQCRevB.5.py", line 76, in <module>
  File "win32com\client\dynamic.py", line 543, in __getattr__
pywintypes.com_error: (-2147221168, 'Could not read key from registry', None, None)

So I checked line 76 in module

TQCmacrowb = xl.Workbooks.Open(os.getcwd()+ "\\" +TQCfilename)

So its trying to open excel workbook. And then I checked what is in dynamic.py line 543

        try:
            ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
        except pythoncom.com_error as details:
            if details.hresult in ERRORS_BAD_CONTEXT:
                # May be a method.
                self._olerepr_.mapFuncs[attr] = retEntry
                return self._make_method_(attr)
            raise
        debug_attr_print("OLE returned ", ret)

Line 543 is ret = self.oleobj.Invoke(retEntry.dispid,0,invoke_type,1) so its already under a try & except. I dont understand why it doesnt catch the error.

Im using Python 3.8.10 32bit, Pyinstaller 4.3, pywin32 301, OS 64bit, Windows 7

My user is using OS 64bit, Windows 10


Solution

  • Issue resolved! The real issue is with the excel VBA. Below is the line that's causing error

      Set Connection = Appl.OpenConnectionByConnectionString("XX.XXX.XX.XXX XX", True)
    

    Excel VBA trying to connect to SAP but there's a problem. I found a solution link below where I have to untick "Notify when a script opens a connection" https://stackoverflow.com/a/37372388/16493039

    SAP Script Notify Option Screenshot