Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Connecting to Outlook via COM

I have the following python code

o = win32com.client.Dispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")
profile = ns.Folders.Item("Profile Name")
tasks = profile.Folders.Item("Tasks")
print tasks.Items

When i run it, the script crashes with this error:

Traceback (most recent call last):
  File "start.py", line 47, in <module>
    o = win32com.client.Dispatch("Outlook.Application")
  File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)

I also tried:

win32com.client.gencache.EnsureDispatch("Outlook.Application")

Not sure what to do and what the problem is

Update: This only happens if Outlook is running, but issuing getActiveObject crashes with 'Operation unavailable'

like image 792
Nuno Furtado Avatar asked Oct 03 '12 09:10

Nuno Furtado


2 Answers

I also faced the same error,

The reason behind it was last time the application called com and didn't quit properly or quitted with some errors.so next time you are unable to invoke it.

i have closed and reopened the outlook and rerun my py code and now it works good.

use this piece of code to avoid getting this error in future

fx = win32com.client.Dispatch('CimplicityME.Application')
try:
    # do stuff
except:
    fx.Quit()

Reference: https://www.mail-archive.com/[email protected]/msg11258.html

like image 113
Vignesh Avatar answered Sep 22 '22 21:09

Vignesh


The error is CO_E_SERVER_EXEC_FAILURE, which most likely means that Outlook is running in a security context different from that of your process. Is either app running with elevated privileges (Run as Administrator)?

When and how does your code run?

Update 2016-Jun-17: Just posting the solution mentioned in the comment to be more visible: run both Outlook and python code either as a regular user or with elevated privileges.

like image 23
Dmitry Streblechenko Avatar answered Sep 21 '22 21:09

Dmitry Streblechenko