franckgaga franckgaga - 1 year ago 276
Python Question

win32com.client.Dispatch works but not win32com.client.gencache.EnsureDispatch

i'm learning win32com for python and I've got a strange problem.

I'e trying to export Outlook Contacts in a List of Dictionnary. My code works perfectly with win32com.client.Dispatch("Outlook.Application). But it returns 0 contacts with win32com.client.gencache.EnsureDispatch("Outlook.Application) that is supposed to be faster and "safer". Here's my code :

class MapiImport():
def __init__(self):
self.olApp = win32com.client.Dispatch("Outlook.Application")
self.namespace = self.olApp.GetNamespace(u"MAPI")
# olFolderContacts = 10 :
self.mapiContacts = self.namespace.GetDefaultFolder(10).Items

def getContacts(self, *fields):
contacts = []
# Class == 40 is ContactItem
# Class == 69 is DistListItem
# Exclude ditribution list and others objects != ContactItem
for contact in filter(lambda x: x.Class == 40,self.mapiContacts) :
if not fields :
ctact = dict((x.Name,x.Value) for x in contact.ItemProperties)
else :
ctact = {}
for field in fields :
itemProp = contact.itemProperties[field]
ctact[field] = itemProp.Value
return contacts

#====TEST SCRIPT====
myMAPI = MapiImport()
fields = (u"LastName",u"FirstName",u"Companies",

So when i replace :

olApp = win32com.client.Dispatch("Outlook.Application")

With :

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

It returns this errors :

Traceback (most recent call last):
File "D:\Documents and Settings\da7950\Mes documents\Dropbox\cheetahImporter\", line 42, in <module>
File "D:\Documents and Settings\da7950\Mes documents\Dropbox\cheetahImporter\", line 19, in getContacts
for contact in filter(lambda x: x.Class == 40,self.mapiContacts) :
File "D:\Documents and Settings\da7950\Mes documents\Python27\lib\site-packages\win32com\gen_py\00062FFF-0000-0000-C000-000000000046x0x9x2\", line 122, in __getitem__
return self._get_good_object_(self._oleobj_.Invoke(*(81, LCID, 1, 1, item)), "Item")
com_error: (-2147352567, "Une exception s'est produite.", (4096, u'Microsoft Office Outlook', u'Index de la matrice en dehors des limites.', None, 0, -2147352567), None)

The message means "Matrix index out of bounds". The strangiest thing is that after I called EnsureDispatch, win32com.client.Dispatch doesn't works anymore. I have to uninstall pywin32 and reinstall it...

I'm running with Python2.7.3 64-bit with Outlook 2007 32-bit


Answer Source

I found a solution. It's a bug win32com makepy...

The main problem is that Outlook is 1-based indexed for olContactItem (in oppsition to 0-based index for python)

olApp = win32com.client.gencache.EnsureDispatch("Outlook.Application")           
namespace = olApp.GetNamespace(u"MAPI")           
# olFolderContacts = 10 :           
mapiContacts = namespace.GetDefaultFolder(10).Items
for i in range(1,len(mapiContacts)+1) :
    contact = self.mapiContacts[i]

There is another problem with contact.itemProperties. All properties are case sensitive with EnsureDispatch, so :


works, but not :


ΒΈ ref for properties names : MSDN

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download