Nuno Furtado
Nuno Furtado

Reputation: 4568

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'

Upvotes: 18

Views: 17107

Answers (6)

gsalimena
gsalimena

Reputation: 11

I had the same error this week, mine was happening because i didnt have outlook installed.

I had "Outlook (new)" and the "Mail and calendar", not the real Outlook.

At the moment i installed, the problem was gone.

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

The error code -2146959355 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.

Upvotes: 17

ashrasmun
ashrasmun

Reputation: 566

What worked for me was closing Outlook prior to invocation of the code. I unfortunately do not understand why that works...

Upvotes: 2

Vignesh
Vignesh

Reputation: 1623

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

Upvotes: 3

Anoj20
Anoj20

Reputation: 31

I had the same issue, I was using a 64 bit installation of Python 2.7. I reinstalled a 32 bit version of Python 2.7 and I was able to use the client dispatch calls.

Upvotes: 2

user1687634
user1687634

Reputation: 11

Try moving your script to another directory and executing it from there. That solved the issue when I encountered it, although I am not sure of the problem's root cause (seems to be an obscure bug with the win32 API, as suggested by Nuno).

Upvotes: 1

Related Questions