Home > Back-end >  Edit Outlook locally saved .msg body by replacing text in VBA
Edit Outlook locally saved .msg body by replacing text in VBA

Time:06-01

Good afternoon,

I have an Outlook .msg email saved at a local folder in my computer.

Is there any way I can replace the word "AAAA" in the body with any word I want in VBA? Is there any way I can change the To: field?

The goal is to run an Excel table and create copies of a template message, replace the To: field and some words of the template with the info in the Excel table and save it. We will manually send latter.

I only need the .msg file modifying code (To: field and body replaces). The loop is already coded.

Thank you so much,

CodePudding user response:

You can use Application.Session.OpenSharedItem to open an MSG file, modify the returned MailItem object (Subject / HTMLBody / Recipients), then call MAilItem.Save to update the MSG file.

CodePudding user response:

The Outlook object model doesn't provide anything to edit MSG files out of the box. But you can automate Outlook to create an item, edit it and then save it back as a template.

Use the Application.CreateItemFromTemplate method which creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item. So, you can create a new item based on the template saved on the disk and then replace everything you need there. Then you could save it back as a template or send the item out. Read more about that in the How To: Create a new Outlook message based on a template article.

  • Related