17

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'

Nuno Furtado
  • 4,548
  • 8
  • 37
  • 57

5 Answers5

16

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • 1
    In my case I was running the script via a Command Prompt window running as administrator, but Outlook was opened as my regular user account. Running the code from a non-admin prompt resolved the issue as suggested. – Ryan D.W. Feb 25 '16 at 17:39
2

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.

Anoj20
  • 31
  • 3
2

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/python-win32@python.org/msg11258.html

Vignesh
  • 1,553
  • 1
  • 10
  • 25
2

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

ashrasmun
  • 490
  • 6
  • 11
1

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).

  • Create a separate question, provide enough details so we can help you. Dont post follow-up questions – Manuel Jul 12 '13 at 15:10
  • Agreed, although my intention was not to ask another question, but to provide a possible solution to anyone else who encounters this problem. I edited my answer for clarification. – user1687634 Jul 12 '13 at 19:23
  • Another directory doesn't seem to be the issue. I've changed permissions and the like. I imagine it's a bug, however some people are still able to connect. – signus Sep 24 '13 at 17:56