win32com.client.Dispatch有效,但不是win32com.client.gencache.EnsureDispatch

时间:2012-07-24 02:54:43

标签: python outlook pywin32 win32com dispatch

我正在学习win32com for python,我遇到了一个奇怪的问题。

我尝试在词典列表中导出Outlook联系人。我的代码与win32com.client.Dispatch(" Outlook.Application)完美配合。但它返回0个联系人,win32com.client.gencache.EnsureDispatch(" Outlook.Application)应该更快,更安全"。这是我的代码:

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
            contacts.append(ctact)
        return contacts

#====TEST SCRIPT====
myMAPI = MapiImport()
fields = (u"LastName",u"FirstName",u"Companies",
          u"HomeTelephoneNumber",u"Home2TelephoneNumber",
          u"MobileTelephoneNumber",
          u"BusinessTelephoneNumber",u"Business2TelephoneNumber",
          u"Email1Address",u"Email2Address",u"Email3Address",
          u"HomeAddress",u"BusinessAddress",
          u"Birthday",u"Anniversary",
          u"Body")
print(myMAPI.getContacts(*fields))

所以当我替换:

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

使用:

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

它返回此错误:

Traceback (most recent call last):
  File "D:\Documents and Settings\da7950\Mes documents\Dropbox\cheetahImporter\mapiImport.py", line 42, in <module>
    print(myMAPI.getContacts(*fields))
  File "D:\Documents and Settings\da7950\Mes documents\Dropbox\cheetahImporter\mapiImport.py", 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\_Items.py", 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)

消息的意思是&#34;矩阵索引越界&#34;。最糟糕的是,在我调用EnsureDispatch之后,win32com.client.Dispatch不再适用了。我必须卸载pywin32并重新安装它......

我使用带有Outlook 2007 32位的64位Python2.7.3

运行

由于

1 个答案:

答案 0 :(得分:3)

我找到了解决方案。这是一个bug win32com makepy ...

主要问题是Outlook是基于1的olContactItem索引(而不是基于0的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]
    ...

contact.itemProperties还有另一个问题。所有属性都区分大小写EnsureDispatch,因此:

contact.ItemProperties("FullName").Value
#       ^

有效,但不是:

contact.itemProperties("FullName").Value
#       ^

要获得正确的名称,请参阅:Microsoft Outlook ContactItem reference model on MSDN