win32com.client.Dispatch 工作但不 win32com.client.gencache.EnsureDispatch [英] win32com.client.Dispatch works but not win32com.client.gencache.EnsureDispatch

查看:45
本文介绍了win32com.client.Dispatch 工作但不 win32com.client.gencache.EnsureDispatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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
            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 Settingsda7950Mes documentsDropboxcheetahImportermapiImport.py", line 42, in <module>
    print(myMAPI.getContacts(*fields))
  File "D:Documents and Settingsda7950Mes documentsDropboxcheetahImportermapiImport.py", line 19, in getContacts
    for contact in filter(lambda x: x.Class == 40,self.mapiContacts) :
  File "D:Documents and Settingsda7950Mes documentsPython27libsite-packageswin32comgen_py0062FFF-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)

该消息的意思是矩阵索引越界".最奇怪的是,在我调用了EnsureDispatch 之后,win32com.client.Dispatch 不再起作用了.我必须卸载pywin32并重新安装它...

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

我正在运行 Python2.7.3 64 位和 Outlook 2007 32 位

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

谢谢

推荐答案

我找到了解决方案.这是一个错误 win32com makepy...

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

主要问题是 Outlook 对于 olContactItem 的索引是基于 1 的(而不是 Python 的基于 0 的索引)

The main problem is that Outlook is 1-based indexed for olContactItem (as opposed 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]
    ...

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

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

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

有效,但无效:

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

要获得正确的名称,请参阅:Microsoft Outlook ContactItemMSDN上的参考模型

To get the names right, consult: Microsoft Outlook ContactItem reference model on MSDN

这篇关于win32com.client.Dispatch 工作但不 win32com.client.gencache.EnsureDispatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆