com_error: (-2147221005, 'Invalid class string', None, None)

Question:

I am trying to connect to Outlook with Python but the following line throws the com_error

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

The complete Traceback is as follows:

    ---------------------------------------------------------------------------
com_error                                 Traceback (most recent call last)
c:usersdipf5appdatalocalprogramspythonpython37libsite-packageswin32comclientdynamic.py in _GetGoodDispatch(IDispatch, clsctx)
     88                 try:
---> 89                         IDispatch = pythoncom.connect(IDispatch)
     90                 except pythoncom.ole_error:

com_error: (-2147221005, 'Invalid class string', None, None)

During handling of the above exception, another exception occurred:

com_error                                 Traceback (most recent call last)
<ipython-input-15-a96a5314c4a3> in <module>
----> 1 outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

c:usersdipf5appdatalocalprogramspythonpython37libsite-packageswin32comclient__init__.py in Dispatch(dispatch, userName, resultCLSID, typeinfo, UnicodeToString, clsctx)
     93   """
     94   assert UnicodeToString is None, "this is deprecated and will go away"
---> 95   dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
     96   return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)
     97 

c:usersdipf5appdatalocalprogramspythonpython37libsite-packageswin32comclientdynamic.py in _GetGoodDispatchAndUserName(IDispatch, userName, clsctx)
    112         else:
    113                 userName = str(userName)
--> 114         return (_GetGoodDispatch(IDispatch, clsctx), userName)
    115 
    116 def _GetDescInvokeType(entry, invoke_type):

c:usersdipf5appdatalocalprogramspythonpython37libsite-packageswin32comclientdynamic.py in _GetGoodDispatch(IDispatch, clsctx)
     89                         IDispatch = pythoncom.connect(IDispatch)
     90                 except pythoncom.ole_error:
---> 91                         IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
     92         else:
     93                 # may already be a wrapped class.

com_error: (-2147221005, 'Invalid class string', None, None)

Any suggestion will be helpful.

Asked By: user13064286

||

Answers:

Looks like Outlook can’t be found on the system. Note, you need to have Outlook installed before running your code.

It is also not clear where and when you are trying to automate Outlook. The Considerations for server-side Automation of Office article states the following:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Answered By: Eugene Astafiev

I ran into this recently myself with Photoshop. Despite the software being installed. It turned out to be a missing registry key entry.

auto fix

try this first, a clean uninstall, restart pc, reinstall. likely will fix any missing registry keys.

Manual fix (complex)

found out this info is stored in the registry. source
you can see all COM commands in ComputerHKEY_LOCAL_MACHINESOFTWAREClasses
if the one you try to run isn’t in there, that likely is why it breaks.

It turned out to be a missing registry entry for
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.ApplicationCLSID
which points to the reg entry in
ComputerHKEY_CLASSES_ROOTCLSID{0a4d56bc-bc92-4b4e-b7db-3577a40c75f7}

There were 2 entries for
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.Application.170
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.Application.170.1
but no entry for
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.Application

since the COM tried to call Photoshop.Application, this failed.
Adding a key for
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.Application
and duplicating the value from the key from
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.Application.170CLSID
to
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesPhotoshop.ApplicationCLSID
fixed this

Answered By: Hannes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.