win32com.client (Howto edit Contacts in Outlook)

Bill Davy Bill at SynectixLtd.com
Fri Jul 4 18:00:38 CEST 2008


"Bill Davy" <Bill at SynectixLtd.com> wrote in message 
news:lPmdnYSbNrM6sPPVnZ2dneKdnZydnZ2d at bt.com...
> "Tim Golden" <mail at timgolden.me.uk> wrote in message 
> news:mailman.61.1215177888.20628.python-list at python.org...
>> Bill Davy wrote:
>>> 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
>>
>> --- hey! that looks familiar :)
>>
>>> 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?).
>>
>> Well that bit's easy: win32com.client.constants is a small class with
>> a __dicts__ attribute (note the "s") which is a list of dictionaries, one
>> per generated library. So you can do something like this:
>>
>> <code>
>> import win32com.client
>>
>> outlook = win32com.client.gencache.EnsureDispatch ("Outlook.Application")
>> outlook_constants = win32com.client.constants.__dicts__[0]
>>
>> for k, v in outlook_constants.items ():
>>  print k, "=>", v
>>
>> </code>
>>
>>> 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.
>>
>> OK.
>>
>>> Where can I find the API?
>>
>> I recommend:
>>
>>  http://msdn.microsoft.com/en-us/library/ms526861.aspx
>>
>> and
>>
>>  http://www.outlookcode.com/article.aspx?id=20
>>
>> and
>>
>>  http://www.cdolive.com/cdo10.htm
>>
>> TJG
>
>
> Brilliant.  But I was a bit disappointed by one experiment.  In MSDN I 
> found "Exploring the Outlook Object Model".  That suggested:
> Outlook | Tools | Macros | VB Editor | View | Object Browser
>
> There I found ContactItems and reckoned I was onto a winner, but found 
> that a "message" from the ContactFolder might have Subject but it did not 
> have a FullName.  Also, if Python crashed, it left Outlook complaing that 
> someone was accessing it but had disappeaared so Outlook has to be 
> restarted each time.
>
> But you have suggested some more things to look at so thanks.  I have had 
> a quick look but have not so far even found "Subject" as a member/property 
> of a message/contact.  I shall keep looking.
>
> Rgds,
>   Bill
>


I kept looking and thought I was saved when I found Receipe 10.16 in the 
Python Cookbook but ....

I changed the following:

                self.oOutlookApp = Dispatch("Outlook.Application")
                #self.oOutlookApp = 
gencache.EnsureDispatch("Outlook.Application")

Because gencache failed and I hade run makepy last week (I only get one day 
a week to look at this).

Then the following failed because (as I found) olFolderContacts is not in 
any of constants' dictionaries.

    ofContacts = onMAPI.GetDefaultFolder(constants.olFolderContacts)

So, sadly, I shall have to put this aside for another week and get on with 
some work.  Any thoughts would be gratefully accepted.

TIA,
   Bill 





More information about the Python-list mailing list