web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Retrieve Emails from o...
Power Automate
Unanswered

Retrieve Emails from outlook

(0) ShareShare
ReportReport
Posted on by 204

Hi Team,

 

When I use the Retrieve Emails from outlook action I would like to amend the .Subject line prior to saving the outlook message.

I loop the emails that I have retrieved but I'm unable to modify the .Subject field. Keep getting errors. 

 

Is there anyway that I'm able to amend the subject line prior to saving the email.

 

Any help is greatly appreciated.

 

thanks

I have the same question (0)
  • VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at

    Hi @christian12 ,

     

    If you want to replace/edit the subject of the retrieved emails using for each. Following is one of the way of doing it: You can use the action "Write to text file" and write the retrieved email parameters to the text.

     

    VishnuReddy1997_0-1712565210576.png

     

    VishnuReddy1997_1-1712565249421.png

     

     

    (Note:- if you got your solution you can mark as solution and gives kudos)

     

    Thanks & Regards

    Vishnu Reddy

  • Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    Hi @christian12 

     

    Please follow the below approach:

     

    1. Launch Outlook: Open Microsoft Outlook.

    2. Retrieve Email Messages: Retrieve email messages from the specified email account and inbox folder. Retrieve all emails based on your condition

    3. Loop(For each) Through Retrieved Emails:

      • For each email message retrieved:
    4. Save Email Messages: Save the current email message as an Outlook message file (.msg) with a custom file name based on the subject of the email(You can add any text- For example : I have added Anytext prior to subject line).

      • Specify the destination folder where the email messages will be saved.
    5. End Loop: Continue looping through all retrieved email messages until all messages have been processed.

     

    Deenuji_0-1712567324120.png

     

    Code:

     

    Outlook.Launch Instance=> OutlookInstance
    Outlook.RetrieveEmailMessages.RetrieveEmails Instance: OutlookInstance Account: $'''Deenu@microsoft.com''' MailFolder: $'''Inbox''' EmailsToRetrieve: Outlook.RetrieveMessagesMode.All MarkAsRead: False ReadBodyAsHtml: False SubjectContains: $'''test''' Messages=> RetrievedEmails
    LOOP FOREACH CurrentItem IN RetrievedEmails
     Outlook.SaveOutlookEmailMessages.SaveEmailsAs Instance: OutlookInstance Account: $'''Deenu@microsoft.com''' EmailsToSave: CurrentItem SaveFormat: Outlook.MessageType.OutlookMessageFormatUnicode SaveEmailsAs: $'''Anytext_%CurrentItem.Subject%''' SaveEmailsTo: $'''C:\\Deenu\\Mails''' MessagesFiles=> StoredMessagesFiles
    END

     

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     
  • christian12 Profile Picture
    204 on at

    Thank you for your help.

     

    Unfortunately what I need to do is save the email as a template file so OFT. 

    I don't need to change the file name, I need to change the subject line within the retrieved email.

     

    So example one of my retrieved emails has a subject line: test123

    I need to amend the subject line for example lets change it to just test. When I save the outlook message my subject line should now read test and not test123.

  • VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at

    Hi @christian12 ,

     

    That what i said if you want to save the outlook message as .msg file you cant do that. But  if you want to save in text file you can do it as I mentioned in first reply.

     

    (Note:- if you got your solution you can mark as solution and gives kudos)

     

    Thanks & Regards

    Vishnu Reddy

  • Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @christian12 

    You can use powershell script to achieve the same. Please refer the below suggested approach:

    Deenuji_0-1712571471730.png

     

    Code(Copy and paste this code into your power automate desktop)

     

    Outlook.Launch Instance=> OutlookInstance
    Outlook.RetrieveEmailMessages.RetrieveEmails Instance: OutlookInstance Account: $'''Deenu@microsoft.com''' MailFolder: $'''Inbox''' EmailsToRetrieve: Outlook.RetrieveMessagesMode.All MarkAsRead: False ReadBodyAsHtml: False SubjectContains: $'''test''' Messages=> RetrievedEmails
    LOOP FOREACH CurrentItem IN RetrievedEmails
     Outlook.SaveOutlookEmailMessages.SaveEmailsAs Instance: OutlookInstance Account: $'''Deenu@microsoft.com''' EmailsToSave: CurrentItem SaveFormat: Outlook.MessageType.OutlookTemplate SaveEmailsAs: $'''test''' SaveEmailsTo: $'''C:\\Deenu\\Mails''' MessagesFiles=> StoredMessagesFiles
     SET MailPath TO $'''C:\\Deenu\\Mails\\test.oft'''
     @@copilotGeneratedAction: 'True'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''$inputVariable = \"%MailPath%\"
    $oftFilePath = $inputVariable
    
    
    # New folder path
    $newFolderPath = \"C:\\Deenu\\Mails\\New\"
    
    # Create the new folder if it doesn\'t exist
    if (-not (Test-Path -Path $newFolderPath)) {
     New-Item -Path $newFolderPath -ItemType Directory | Out-Null
    }
    
    # Create an Outlook Application object
    $outlook = New-Object -ComObject Outlook.Application
    
    # Open the OFT file as a new mail item
    $mailItem = $outlook.CreateItemFromTemplate($oftFilePath)
    
    # Extract existing subject
    $subject = $mailItem.Subject
    
    # Modify subject
    $newSubject = \"Your New Subject Here\"
    $mailItem.Subject = $newSubject
    
    # Build the new file path
    $newFilePath = Join-Path -Path $newFolderPath -ChildPath (Split-Path -Leaf $oftFilePath)
    
    # Save the modified email with the same file name and extension in the new folder
    $mailItem.SaveAs($newFilePath)
    
    # Close the mail item
    $mailItem.Close()
    
    # Release COM objects
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
    Remove-Variable outlook -ErrorAction SilentlyContinue''' ScriptOutput=> PowershellOutput
    END

     

     

    New to power automate desktop? Not sure how to copy and paste the above suggested code into your desktop flow? Please refer the below image:

    Deenuji_1-1712571588528.gif

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     
  • christian12 Profile Picture
    204 on at

    Thank you. Unfortunately I have amended the script with the correct details and it doesn't create the New folder with the amended email. Unsure what piece of the code is failing. Tried to create the folder manually and no additional email is saving.

     

     

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @christian12 

     

    Please make sure to create the similar folders or rename it as required:

    Deenuji_1-1712575878355.png

     

    Deenuji_0-1712575850547.png

     

     

    Please use the below updated code[copy and paste into your PAD]:

     

    Outlook.Launch Instance=> OutlookInstance
    Outlook.RetrieveEmailMessages.RetrieveEmails Instance: OutlookInstance Account: $'''Deenu@microsoft.com''' MailFolder: $'''Inbox''' EmailsToRetrieve: Outlook.RetrieveMessagesMode.All MarkAsRead: False ReadBodyAsHtml: False SubjectContains: $'''RPA Extraction Reports''' Messages=> RetrievedEmails
    LOOP FOREACH CurrentItem IN RetrievedEmails
     Outlook.SaveOutlookEmailMessages.SaveEmailsAs Instance: OutlookInstance Account: $'''Deenu@microsoft.com''' EmailsToSave: CurrentItem SaveFormat: Outlook.MessageType.OutlookTemplate SaveEmailsAs: CurrentItem.Subject SaveEmailsTo: $'''C:\\Deenu\\Mails''' MessagesFiles=> StoredMessagesFiles
     SET MailPath TO $'''C:\\Deenu\\Mails\\%CurrentItem.Subject%.oft'''
     @@copilotGeneratedAction: 'True'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''$inputVariable = \"%MailPath%\"
    $oftFilePath = $inputVariable
    
    
    # New folder path
    $newFolderPath = \"C:\\Deenu\\Mails\\New\"
    
    # Create the new folder if it doesn\'t exist
    if (-not (Test-Path -Path $newFolderPath)) {
     New-Item -Path $newFolderPath -ItemType Directory | Out-Null
    }
    
    # Create an Outlook Application object
    $outlook = New-Object -ComObject Outlook.Application
    
    # Open the OFT file as a new mail item
    $mailItem = $outlook.CreateItemFromTemplate($oftFilePath)
    
    # Extract existing subject
    $subject = $mailItem.Subject
    
    # Modify subject
    $newSubject = \"Your New Subject Here\"
    $mailItem.Subject = $newSubject
    
    # Build the new file path
    $newFilePath = Join-Path -Path $newFolderPath -ChildPath (Split-Path -Leaf $oftFilePath)
    
    # Save the modified email with the same file name and extension in the new folder
    $mailItem.SaveAs($newFilePath)
    
    # Close the mail item
    $mailItem.Close()
    
    # Release COM objects
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
    Remove-Variable outlook -ErrorAction SilentlyContinue''' ScriptOutput=> PowershellOutput
    END

     

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     
  • christian12 Profile Picture
    204 on at

    Thank you. Fixed it and its now working. Next issue I have is that its not creating a new email with amended subject line. New folder is empty and the original email is saved. Nothing has been amended.

  • Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @christian12

    Please verify that the name of the email saved in the folder matches what you are passing to the PowerShell script. Alternatively, try hardcoding the email name while saving, such as "test123.oft," and ensure that you pass the same name to the PowerShell script(just testing purpose).

     

    Deenuji_0-1712582420898.png

    Deenuji_1-1712582489290.png

     

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     
  • christian12 Profile Picture
    204 on at

    Thank you got it working. Do you know if its possible straight from Power automate cloud flow to save emails as OFT format.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard