win32com.client (Howto edit Contacts in Outlook)

Bill Davy Bill at SynectixLtd.com
Fri Jul 11 09:48:13 EDT 2008


"Tim Golden" <mail at timgolden.me.uk> wrote in message 
news:mailman.325.1215781777.20628.python-list at python.org...
> Bill Davy wrote:
>> and since then have been busy with work, and my other job, and the 
>> garden.
>
> Aha! So you're English, are you? Looks like you're in the West Country.
> Weather map suggests you're not short of rain over there :)
>
>> Now I am back looking at this (and using WInUSB to talk to a Maxim 3421E 
>> etc etc but that's another story).  So any help today will be much 
>> appreciated.
>> Rgds,
>
> Can't remember what the particular obstacles were you
> were facing, but this runs OK on my setup -
> Python 2.5.2 / pywin32 211 / Outlook 2003:
>
> <code>
> import os, sys
> import win32com.client
> constants = win32com.client.constants
>
> def items (contacts):
>  items = contacts.Items
>  item = items.GetFirst ()
>  while item:
>    yield item
>    item = items.GetNext ()
>
> #
> # Add whatever fields you like from:
> # http://msdn.microsoft.com/en-us/library/aa210907(office.11).aspx
> #
> FIELDS = ['FullName', 'CompanyName', 'Email1Address']
>
> outlook = win32com.client.gencache.EnsureDispatch ("Outlook.Application")
> ns = outlook.GetNamespace ("MAPI")
> for contact in items (ns.GetDefaultFolder (constants.olFolderContacts)):
>  if contact.Class == constants.olContact:
>    print contact
>    for field in FIELDS:
>      print "  ", field, "=>", getattr (contact, field, "<Unknown>")
>
> </code>
>
> Hope that helps.
> TJG


jUST IN CASE,. i CUT'NPASTED THE PROGRAM:

import os, sys
import win32com.client
constants = win32com.client.constants

def items (contacts):
  items = contacts.Items
  item = items.GetFirst ()
  while item:
    yield item
    item = items.GetNext ()

#
# Add whatever fields you like from:
# http://msdn.microsoft.com/en-us/library/aa210907(office.11).aspx
#
FIELDS = ['FullName', 'CompanyName', 'Email1Address']

outlook = win32com.client.gencache.EnsureDispatch ("Outlook.Application")
ns = outlook.GetNamespace ("MAPI")
for contact in items (ns.GetDefaultFolder (constants.olFolderContacts)):
  if contact.Class == constants.olContact:
    print contact
    for field in FIELDS:
      print "  ", field, "=>", getattr (contact, field, "<Unknown>")

---------------------------------------------------------------------------------------------------
And then I ran it:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] 
on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 1.2.2
>>> ================================ RESTART 
>>> ================================
>>>

Traceback (most recent call last):
  File "H:/Personal/OutlookIF1/t2.py", line 18, in <module>
    outlook = win32com.client.gencache.EnsureDispatch 
("Outlook.Application")
  File "C:\Python25\Lib\site-packages\win32com\client\gencache.py", line 
536, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], 
bForDemand=bForDemand)
  File "C:\Python25\Lib\site-packages\win32com\client\gencache.py", line 
393, in EnsureModule
    module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
  File "C:\Python25\Lib\site-packages\win32com\client\gencache.py", line 
262, in GetModuleForTypelib
    AddModuleToCache(typelibCLSID, lcid, major, minor)
  File "C:\Python25\Lib\site-packages\win32com\client\gencache.py", line 
554, in AddModuleToCache
    dict = mod.CLSIDToClassMap
AttributeError: 'module' object has no attribute 'CLSIDToClassMap'
>>>

-------------------------------------------------------------------------------------------------

Outlook is running fine.

This is how the fucntion where the failure occurs begins:

def AddModuleToCache(typelibclsid, lcid, major, minor, verbose = 1, 
bFlushNow = not is_readonly):
 """Add a newly generated file to the cache dictionary.
 """
 fname = GetGeneratedFileName(typelibclsid, lcid, major, minor)
 mod = _GetModule(fname)
 # if mod._in_gencache_ is already true, then we are reloading this
 # module - this doesn't mean anything special though!
 mod._in_gencache_ = 1
 dict = mod.CLSIDToClassMap
 info = str(typelibclsid), lcid, major, minor
 for clsid, cls in dict.items():
  clsidToTypelib[clsid] = info

-----------------------------------------------------------------------------------------------

Yes, we have suffiicient rain but the gaden needed it.  I am about to become 
the proud tenant of half an allotment.  Still, this time last year we had 
floods.

TIA,
   Bill 





More information about the Python-list mailing list