win32com.client (Howto edit Contacts in Outlook)

Tim Golden mail at timgolden.me.uk
Fri Jul 11 15:09:24 CEST 2008


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



More information about the Python-list mailing list