Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying Microsoft Outlook contacts from Python

I have written a few Python tools in the past to extract data from my Outlook contacts. Now, I am trying to modify my Outlook Contacts. I am finding that my changes are being noted by Outlook, but they aren't sticking. I seem to be updating some cache, but not the real record.

The code is straightforward.

import win32com.client
import pywintypes

o = win32com.client.Dispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")
profile = ns.Folders.Item("My Profile Name")
contacts = profile.Folders.Item("Contacts")
contact = contacts.Items[43] # Grab a random contact, for this example.
print "About to overwrite ",contact.FirstName, contact.LastName
contact.categories = 'Supplier' # Override the categories

# Edit: I don't always do these last steps.
ns = None 
o = None

At this point, I change over to Outlook, which is opened to the Detailed Address Cards view.

I look at the contact summary (without opening it) and the category is unchanged (not refreshed?).

I open the contact and its category HAS changed, sometimes. (Not sure of when, but it feels like it is cache related.) If it has changed, it prompts me to Save Changes when I close it which is odd, because I haven't changed anything in the Outlook UI.

If I quit and restart Outlook, the changes are gone.

I suspect I am failing to call SaveChanges, but I can't see which object supports it.

So my question is:

  • Should I be calling SaveChanges? If so, where is it?
  • Am I making some other silly mistake, which is causing my data to be discarded?
like image 904
Oddthinking Avatar asked Jan 02 '09 00:01

Oddthinking


People also ask

How do I edit contacts in Outlook 2010?

Edit ContactsDouble-click a contact to edit the contact's information. The Contact dialog box will appear. Edit the information as desired.


1 Answers

I believe there is a .Save() method on the contact, so you need to add:

contact.Save()

like image 86
hfcs101 Avatar answered Sep 23 '22 18:09

hfcs101