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
- Copy rename of msg file into new folder at different location.
- Edit the desire body message values
- Creating a draft file within Outlook
Following is what I failed to achieve and need assistance with.
- Change of Subject Line
- Saving of Document outside of Outlook
Following is what I would expected to achieve
- 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
Instead of using
Application.CreateItemFromTemplate(which does what its name suggests), useNamespace.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 ofMailItem.Save.