Errorum
Errorum

Reputation: 223

Editing Outlook Templates with Powershell

I'm trying to edit an often used email template, then send it automatically. I want it to overwrite the same .msg file for others to use (they may not be code savvy). However, the code below just saves the object info into the .msg file. How do I get it to save as an actual email template?

$file= <file location>
    $outlook= New-Object -ComObject outlook.application
    $msg= $outlook.createitemfromtemplate($file)
    $msg.body= $msg.body -replace "DB\d*", "DB$a"
    $msg|out-file $file

I'll also be attaching two files. I've seen this done using the smtp server, but not with the COM object method I'm using. Bonus points if you can point me in the right direction there as well.

Upvotes: 0

Views: 3149

Answers (2)

Mike Garuccio
Mike Garuccio

Reputation: 2718

You'll need to use the save() method rather than outputting the object to a file.

$msg.save()

Upvotes: 1

Ranadip Dutta
Ranadip Dutta

Reputation: 9123

-Force overwrites the existing file forcefully. If you want to append the data in the same file then you can use "-Append" also.

Instead of

$msg|out-file $file

Do this:

$msg|out-file $file -Force

Upvotes: 0

Related Questions