win32com.client (Howto edit Contacts in Outlook)

Bill Davy Bill at SynectixLtd.com
Fri Jul 4 14:25:33 CEST 2008


I am trying to edit Contacts in Outlook.  This is so I can transfer numbers 
from my address book which is an Excel spreadsheet to my mobile phone.  I 
came across the following snippet of code which enabled me to the contacts 
at least list.  I had to root around to discover CdoDefaultFolderContacts 
(though it was guessable; how could I enumerate win32com.client.constants?).



I now want to work through the Contacts in Outlook patching in data from my 
spreadsheet, and also making new contacts where there is an entry in my 
spreadsheet which has not gone into Contacts already.



Where can I find the API?  I downloaded OutlookSpy but it is not clear to me 
that it can display the structure of the data, nor does it list methods for 
objects (a Contact, a Folder of Contacts) that I had hoped.



TIA,

    Bill



class Folder (object):
  def __init__ (self, folder):
    self._folder = folder
  def __getattr__ (self, attribute):
    return getattr (self._folder, attribute)
  def __iter__ (self):
    #
    # NB You *must* collect a reference to the
    # Messages collection here; otherwise GetFirst/Next
    # resets every time.
    #
    messages = self._folder.Messages
    message = messages.GetFirst ()
    while message:
      yield message
      message = messages.GetNext ()

if __name__ == '__main__':
  import win32com.client
  session = win32com.client.gencache.EnsureDispatch ("MAPI.Session")
  constants = win32com.client.constants
  session.Logon ("Outlook")

  #
  # CdoDefaultFolderInbox
  # CdoDefaultFolderSentItems
  # CdoDefaultFolderContacts
  #
  contact_items = Folder (session.GetDefaultFolder 
(constants.CdoDefaultFolderContacts))
  for message in contact_items:
    print message
    sys.exit(1)
    print message.Subject 





More information about the Python-list mailing list