Notifications
Announcements
# This code has been generated by AI. Original prompt: # Powershell script to convert .msg saved email to pdf # Add-Type -AssemblyName Microsoft.Office.Interop.Outlook Add-Type -AssemblyName System.Windows.Forms function Convert-MsgToPdf { param ( [string]$msgFilePath, [string]$pdfFilePath ) $outlook = New-Object -ComObject Outlook.Application $namespace = $outlook.GetNamespace("MAPI") $mailItem = $namespace.OpenSharedItem($msgFilePath) $wordApp = New-Object -ComObject Word.Application $wordApp.Visible = $false $tempHtmlPath = [System.IO.Path]::ChangeExtension($msgFilePath, ".html") $mailItem.SaveAs($tempHtmlPath, [Microsoft.Office.Interop.Outlook.OlSaveAsType]::olHTML) $document = $wordApp.Documents.Open($tempHtmlPath) $document.SaveAs([ref] $pdfFilePath, [ref] 17) # 17 is the wdFormatPDF enumeration $document.Close([ref] $false) $wordApp.Quit() Remove-Item $tempHtmlPath [System.Runtime.Interopservices.Marshal]::ReleaseComObject($document) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wordApp) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($namespace) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null } # Example usage $msgFilePath = "%CurrentItem.FullName%" $pdfFilePath = "C:\Users\Desktop\Power Automate Desktop\Practice\HTML\Email Messages as .pdf\Email_%Count%.pdf" Convert-MsgToPdf -msgFilePath $msgFilePath -pdfFilePath $pdfFilePath