|
From: Bill Davy on 4 Jul 2008 08:25 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
From: Tim Golden on 4 Jul 2008 09:24 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
From: Bill Davy on 4 Jul 2008 10:03 "Tim Golden" <mail(a)timgolden.me.uk> wrote in message news:mailman.61.1215177888.20628.python-list(a)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
From: Bill Davy on 4 Jul 2008 12:00 "Bill Davy" <Bill(a)SynectixLtd.com> wrote in message news:lPmdnYSbNrM6sPPVnZ2dneKdnZydnZ2d(a)bt.com... > "Tim Golden" <mail(a)timgolden.me.uk> wrote in message > news:mailman.61.1215177888.20628.python-list(a)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
From: Tim Roberts on 6 Jul 2008 00:59
"Bill Davy" <Bill(a)SynectixLtd.com> 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. Are you actually running Outlook? Your news posting was made from Outlook Express, and Outlook Express cannot be controlled by COM (although MAPI works). -- Tim Roberts, timr(a)probo.com Providenza & Boekelheide, Inc. |