Modifying Microsoft Outlook contacts from Python

我的未来我决定 提交于 2019-12-17 22:37:13

问题


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?

回答1:


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

contact.Save()



来源:https://stackoverflow.com/questions/405724/modifying-microsoft-outlook-contacts-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!