18

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'

4
  • I have the same issue, for dispatching Powerpoint thou. It appears only when Powerpoint was running previously. Commented May 3, 2013 at 14:26
  • Have you ever figured it out? Commented May 3, 2013 at 14:26
  • I've never figured it out, seems to be a bug in the win32com API Commented Jun 4, 2013 at 15:42
  • What operating system are you running? I started having this issues after migration to win10 and I am wondering if it is related. Commented Feb 10, 2020 at 11:13

6 Answers 6

17

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Same as above. Re-Installing Anaconda for user (rather than public) resolved the issue.
4

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

Comments

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.

Comments

2

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

Comments

2

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.

2 Comments

Please clean up punctuation, wording, and capitalization. Other than that, this answer looks good to me :)
Co-worker just hit this issue. Was working fine for me. Saw this, so I switched on the "Try the new Outlook" switch and boom reproduced their issue. Turned the switch off, and everything started working again. :+1:
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).

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.