简体   繁体   中英

Python function for changing the subject line of an Outlook mail item

Is there a Python function for changing the subject line of an Outlook mail item?

I have seen solutions for this using VBA. Was looking for a function/ method in python.

Thanks

Update : Here's what i am trying :

import win32com.client as win32
outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mapi = outlook.GetNamespace('MAPI')
folder = mapi.GetDefaultFolder(6)
messages = folder.Items

# Change the current subject line to 'Testing subject change'
messages.GetFirst().Subject = 'Testing subject change'

However, the subject line doesn't change. Is there any specific function i should be using?

This short piece of code will replace all Subject lines of all emails in the specified folder, in this case "Drafts" (provided your Office is in English)

import win32com.client as win32

outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")
acc = outlook.Folders("myaddress@provider.com")
eMailFolder = acc.folders("Drafts") #This is the localized name of your folder, as it appears in Outlook's GUI

def replaceSubjectLine(email:object):
        print(email.Subject)
        email.Subject = "This is the new subject line"
        email.Save
        print(email.Subject)

for message in eMailFolder.Items:
    replaceSubjectLine(message)

In Short: You read in the MailItem Object into Python, then changed one of its Properties (Subject), but you never .Save the changed MailItem to Outlook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM