Outlook comobject overwrites files without prompt

Viewed 23

I'm writing a little script that would batch convert .msg files to .pdf with .html in the middle, all while saving attachments. While this is a work in progress I encountered a strange thing. If a file with the same .fullname as the attachment is already present in the folder it gets overwritten, no prompt. Is there a way to force confirmation on .SaveAsFile method for the attachments? See current script below.

$o = New-Object -ComObject outlook.application
$1 = "C:\Program Files\Google\Chrome\Application\chrome.exe"
Set-Alias -Name chrome -Value $1

Get-ChildItem -file -filter *.msg | ForEach-Object {

    $msgFullname = $_.FullName
    $htmlname = $msgFullname -replace '.msg', '.html'
    $attloc = $_.Directory

    $msg = $o.CreateItemFromTemplate($msgFullname)
    if ($msg.Attachments.Count -ge 1) {
        $msg.Attachments | ForEach-Object {
            $attname = $attloc.FullName + "\" + $_.FileName
            $_.SaveAsFile($attname)
        }
    }
    $msg.SaveAs($htmlname, 5)

    $dest = $_.DirectoryName + "\" + $_.BaseName + ".pdf"
    $source = $_.DirectoryName + "\" + $_.BaseName + ".html"

    chrome --headless --disable-gpu --print-to-pdf=$dest  $source --print-to-pdf-no-header

    $1 = $_.BaseName

    Get-ChildItem | Where-Object { ($_.PSIsContainer -eq $true -and $_.BaseName -like "*$1*") -OR $_.Name -like ($_.BaseName + "*.html") } | Remove-Item -Recurse -Force
}
1 Answers

No, there is no prompt, and there shouldn't be one - if you are programmatically saving a message/attachment, it is your responsibility to check if the file already exists and what to do with it.

Related