Question

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?
Was it helpful?

Solution

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

contact.Save()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top