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

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

我正在尝试使用 Python 连接到 Outlook,但以下行抛出 com_error

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

完整的Traceback如下:

    ---------------------------------------------------------------------------
com_error                                 Traceback (most recent call last)
c:\users\dipf5\appdata\local\programs\python\python37\lib\site-packages\win32com\client\dynamic.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:\users\dipf5\appdata\local\programs\python\python37\lib\site-packages\win32com\client\__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:\users\dipf5\appdata\local\programs\python\python37\lib\site-packages\win32com\client\dynamic.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:\users\dipf5\appdata\local\programs\python\python37\lib\site-packages\win32com\client\dynamic.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)

任何建议都会有所帮助。

系统中似乎找不到 Outlook。请注意,您需要先安装 Outlook 运行 您的代码。

也不清楚您何时何地尝试自动化 Outlook。 Considerations for server-side Automation of Office 文章陈述如下:

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.