1

I am using python to automate some outlook email replies and I need to remove the "RE:" from the subject when using the email.ReplyAll(), I tried to do email.Subject = subject before replying but it still adds the "RE:".

FYI, I do not intend to remove the default functionality of Outlook to add "RE:" when replying to emails. I only intend to remove it when using the python script.


import win32com.client

num_emails_limit = 10 #Loops through last 10 emails in folder
folder_name = 'Sent Items' #Name of Outlook folder I want to search

#Opens Outlook application
outlook_application = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
outlook_mailbox = outlook_application.Folders.Item(1)

#Finds the correct Outlook Folder by searching for a folder that is equal to folder_name
for folder in outlook_mailbox.Folders:
    if folder.Name == folder_name:
        found_folder = folder

Filter = "[Subject] = 'SomeWord'"
folder_emails = found_folder.Items.Restrict(Filter)

folder_emails.Sort("[ReceivedTime]", True)

#Loops Through Outlook Emails
counter = 0
for email in folder_emails:
    try:
        email_subject = str(email.Subject)
        print('Subject: ' + email_subject)
        counter = counter +1
    except:
        pass

    #If the email I want to reply to contains "SomeWord"
    if "SomeWord" in email.Subject:
        #Change the subject to "OtherWord"
        subject = "OtherWord"
        email.Subject = subject
        email.ReplyAll().Display()
    
    if counter == num_emails_limit:
        break

So using this code the Subject of the email will be "RE: OtherWord" instead of just "OtherWord". Is there any way to fix this?

jks
  • 13
  • 6

1 Answers1

0

You need to set the Subject property on the new reply email, not source one, for example:

email.Subject = subject
email.ReplyAll().Display()

The ReplyAll method creates a reply to all original recipients from the original message. A MailItem object that represents the reply s returned back. That is where you need to change the Subject proerty by removing the prefix:

email.Subject = subject
reply = email.ReplyAll()
reply.Subject = subject
reply.Display()

Also I've noticed that your code iterates over all items in the folder:

for email in folder_emails:

Instead, you need to use the Find/FindNext or Restrict methods of the Items class where you could deal only with items that correspond to the search criteria. Read more about these methods in the articles I wrote for the technical blog:

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • I am using the Restrict method with the Subject filter but I need to somehow find the email that CONTAINS a string instead of the subject being equal to a string and I cannot get it to work. This is what I have: **Filter = "[Subject] = 'SomeString'"** and then **folder_emails = found_folder.Items.Restrict(Filter)**. I've updated the code of the original post so it's easier for you to see. – jks Mar 17 '23 at 23:00
  • The DASL query doesn't support that. You need to use the JET syntax. For example: `filter = "@SQL=" & Chr(34) & "https://schemas.microsoft.com/mapi/proptag/0x0037001E" _ & Chr(34) & " ci_phrasematch " & "'can''t'"` – Eugene Astafiev Mar 19 '23 at 16:46
  • Thank you for your response, however I just read that you cannot do that, check this: https://learn.microsoft.com/en-us/office/vba/api/outlook.items.restrict#additional-notes I'll have to search item by item to look for an email which contains a specific word in the subject, which was how I was doing it before. – jks Mar 20 '23 at 12:29
  • Add the property to the folder fields if it doesn't work. – Eugene Astafiev Mar 20 '23 at 12:45