Edit Outlook .msg file - how to save it from Outlook Draft to desire location using Powershell

1.6k Views Asked by At

Given: Using Powershell Copy folder with template file to different directory Re-name the .msg file and edit .msg file for recurring common values and Save.

Problem: The outlook open the file and save it in the Outlook draft folder rather than editing and saving it to given copied directory.

On google search, I found the Word document editing programming but not for Outlook. In word document programming we could search specific word and replace it in the document.

I am not sure if similar could be achieved with Outlook or not.

Following is what I successful to achieve

  1. Copy rename of msg file into new folder at different location.
  2. Edit the desire body message values
  3. Creating a draft file within Outlook

Following is what I failed to achieve and need assistance with.

  1. Change of Subject Line
  2. Saving of Document outside of Outlook

Following is what I would expected to achieve

  1. Changing the subject line and replacing common values using find and replace feature outside of Outlook program using Powershell.

Requesting assistance.

Thank you all for those who give this their thoughts and efforts in advance.

The code is as follows

$LBStempfolder = Template folder location
$newfolder = new directory location

If(test-path $LBStempfolder)
{ if(!(test-path $newfolder))
{ $newfolder = New-Item -ItemType "Directory" -Path "LBStempfolder" -Name "XYZ" -Confirm
Write-host $newfolder
if(test-path $newfolder)
{ Copy-Item -Path $LBStempfolder -Destination $newfolder
Get-ChildItem -Path $newfolder\* -Include xyz\* -Recurse |
Rename-Item -NewName {$_.Name -replace 'xyz', 'abc'} -Force
}}}
Function Get-Outlookfile ($ofile)

{ $outlook = New-Object -ComObject outlook. Application
$namespace = $outlook.GetNameSpace("MAPI")
$email = $outlook.CreateItemFromTemplate($ofile)
$email.HTMLBody = $email.HTMLBody.Replace("XXX-XXX", "myedition")
$email.Subject = $email.Subject("mysubject")
$email.Save()

}
$outlook doc = Get-Outlookfile -ofile $newfolder\\abc.msg
2

There are 2 best solutions below

8
Dmitry Streblechenko On

Instead of using Application.CreateItemFromTemplate (which does what its name suggests), use Namespace.OpenSharedItem:

$email = $outlook.Session.OpenSharedItem($ofile)

or

$email = $outlook.GetNamespace("MAPI").OpenSharedItem($ofile)).

When saving an item, you might also want to use MailItem.SaveAs(.., olMsgUnicode) instead of MailItem.Save.

6
Eugene Astafiev On

The Save method of the MailItem class saves the an Outlook item to the current folder in Outlook (not disk) or, if this is a new item, to the Outlook default folder for the item type, in your case it would be the Drafts folder because it has never been saved in Outlook.

If you need to save the created from a template and edited item to the disk, you need to use the MailItem.SaveAs method which saves the Microsoft Outlook item to the specified path on the disk and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used.

So, the code may look like this:

$LBStempfolder = Template folder location
$newfolder = new directory location

If(test-path $LBStempfolder)
{ if(!(test-path $newfolder))
{ $newfolder = New-Item -ItemType "Directory" -Path "LBStempfolder" -Name "XYZ" -Confirm
Write-host $newfolder
if(test-path $newfolder)
{ Copy-Item -Path $LBStempfolder -Destination $newfolder
Get-ChildItem -Path $newfolder\* -Include xyz\* -Recurse |
Rename-Item -NewName {$_.Name -replace 'xyz', 'abc'} -Force
}}}
Function Get-Outlookfile ($ofile)

{ $outlook = New-Object -ComObject outlook. Application
$namespace = $outlook.GetNameSpace("MAPI")
$email = $outlook.CreateItemFromTemplate($ofile)
$email.HTMLBody = $email.HTMLBody.Replace("XXX-XXX", "myedition")
$email.Subject = $email.Subject("mysubject")
$email.SaveAs($ofile)

}
$outlook doc = Get-Outlookfile -ofile $newfolder\\abc.msg

In some case you may be interested in using the NameSpace.OpenSharedItem method which opens a shared item from a specified path or URL. See Import Saved Items using OpenSharedItem for more information.

In both cases you need to use the SaveAs method save your changes on the disk, so the original item will be overwritten.

But the difference between these two methods is that the CreateItemFromTemplate method creates a new item in a composed state, to save it as a received item you need to do extra steps for that, while the OpenSharedItem method doesn't change the item state. If that makes sense. Otherwise, both ways are fully valid.