Dim objItem, objFSO, strFile, input, fileExt, strHtml, strPdf, msg, wordDoc, wordApp, tempFileFolder
Const olFormatHTML = 5
Const wdFormatPDF = 17
input = Wscript.Arguments(0)
' Create a File System object
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
' Check if the Word document exists
If objFSO.FileExists(input) Then
Set objItem = objFSO.GetFile(input)
strFile = objItem.Path
Else
WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
WScript.Quit
End If
fileExt = Right(strFile,3)
If fileExt <> "msg" Then
WScript.Echo "FILE ERROR: The file extension is not .msg" & vbCrLf
WScript.Quit
End If
strHtml = objItem.Path + ".html"
strPdf = objItem.Path + ".pdf"
Set Outlook = CreateObject("Outlook.Application")
Set msg = Outlook.CreateItemFromTemplate(objItem.Path)
msg.SaveAs strHtml, olFormatHTML
Outlook.Quit
Set wordApp = CreateObject( "Word.Application" )
wordApp.Documents.Open strHtml
Set wordDoc = wordApp.ActiveDocument
wordDoc.SaveAs strPdf, wdFormatPDF
wordDoc.Close
wordApp.Quit
If objFSO.FileExists(strHtml) Then
objFSO.DeleteFile(strHtml)
End If
tempFileFolder = objItem.Path & "_files"
If objFSO.FolderExists(tempFileFolder) Then
objFSO.DeleteFolder(tempFileFolder)
End If