Option Explicit Dim blnOverwrite Dim intConvertedFiles, intMatchingFiles, intOverwriteErrors Dim objParentFolder, objFSO Dim strExtension, strFile, strFileName, strMsg, strParentFolder, strPDFFile, strWordFile blnOverwrite = False Set objFSO = CreateObject( "Scripting.FileSystemObject" ) With objFSO ' Command Line Parsing Select Case WScript.Arguments.Unnamed.Count Case 0: Syntax Null Case 1: strWordFile = .GetAbsolutePathName( WScript.Arguments.Unnamed(0) ) strPDFFile = .BuildPath( objFSO.GetParentFolderName( strWordFile ), .GetBaseName( strWordFile ) & ".pdf" ) strExtension = .GetExtensionName( strWordFile ) strFileName = .GetBaseName( strWordFile ) strParentFolder = .GetParentFolderName( strWordFile ) Case 2: strWordFile = .GetAbsolutePathName( WScript.Arguments.Unnamed(0) ) If InStr( strWordFile, "*" ) Then Syntax "No wildcards allowed in Word file specification if PDF file is specified." Else strPDFFile = .GetAbsolutePathName( WScript.Arguments.Unnamed(1) ) End If Case Else: Syntax "Invalid command line arguments." End Select Select Case WScript.Arguments.Named.Count Case 0: ' OK Case 1: If WScript.Arguments.Named.Exists( "O" ) Then blnOverwrite = True Else Syntax "Invalid command line switch." End If Case 2: If WScript.Arguments.Named.Exists( "O" ) Then Syntax "Invalid command line switch." Else Syntax "Invalid command line switches." End If Case Else: Syntax "Invalid command line switches." End Select ' Command Line Validation If InStr( strWordFile, "?" ) Then Syntax "Invalid wildcard(s) in specified Word document" End If If InStr( strWordFile, "*" ) Then If strFileName = "*" And Not InStr( strExtension, "*" ) And Not InStr( strParentFolder, "*" ) Then intMatchingFiles = 0 For Each strFile In .GetFolder( strParentFolder ).Files If LCase( .GetExtensionName( strFile ) ) = LCase( strExtension ) Then intMatchingFiles = intMatchingFiles + 1 End If Next If intMatchingFiles = 0 Then Syntax "No files matching Word documents specification" End If Else Syntax "Invalid wildcard(s) in specified Word document" End If Else If Not .FileExists( strWordFile ) Then Syntax "The specified Word document does not exist." End If If InStr( strPDFFile, "*" ) Then Syntax "No wildcards allowed in PDF file specification." End If If Not .FolderExists( .GetParentFolderName( strPDFFile ) ) Then Syntax "The parent folder for the specified output file does not exist." End If If Not LCase( .GetExtensionName( strPDFFile ) ) = "pdf" Then Syntax "The specified PDF file must have a "".pdf"" extension." End If If Not blnOverwrite Then If .FileExists( strPDFFile ) Then Syntax "The specified PDF file already exists." & vbCrLf & vbTab & "Use /O to silently overwrite existing PDF files." End If End If End If ' The actual conversion is done by the Doc2PDF subroutine If InStr( strWordFile, "*" ) Then intOverwriteErrors = 0 intConvertedFiles = 0 strMsg = "" For Each strFile In .GetFolder( .GetParentFolderName( strWordFile ) ).Files If LCase( .GetExtensionName( strFile ) ) = LCase( .GetExtensionName( strWordFile ) ) Then strPDFFile = .BuildPath( strParentFolder, .GetBaseName( strFile ) & ".pdf" ) If Not blnOverwrite And .FileExists( strPDFFile ) Then intOverwriteErrors = intOverwriteErrors + 1 strMsg = strMsg & "ERROR:" & vbTab & """" & .GetFileName( strPDFFile ) & """ already exists." & vbCrLf & vbTab & "Use /O to silently overwrite existing PDF files." & vbCrLf Else intConvertedFiles = intConvertedFiles + 1 strMsg = strMsg & "Converting """ & .GetFileName( strFile ) & """ . . ." & vbCrLf Doc2PDF .GetAbsolutePathName( strFile ), strPDFFile End If End If Next WScript.Echo strMsg & vbCrLf & intConvertedFiles & " documents successfully converted, " & intOverwriteErrors & " existing PDF files were skipped." & vbCrLf If intOverwriteErrors > 0 Then Syntax intOverwriteErrors & " existing PDF files were skipped." & vbCrLf & vbTab & "Use /O to silently overwrite existing PDF files." End If Else Doc2PDF strWordFile, strPDFFile End If End With ' Finished Set objFSO = Nothing Sub Doc2PDF( myWordFile, myPDFFile ) Dim objDoc, objWord Const wdFormatDocument = 0 Const wdFormatDocument97 = 0 Const wdFormatDocumentDefault = 16 Const wdFormatDOSText = 4 Const wdFormatDOSTextLineBreaks = 5 Const wdFormatEncodedText = 7 Const wdFormatFilteredHTML = 10 Const wdFormatFlatXML = 19 Const wdFormatFlatXMLMacroEnabled = 20 Const wdFormatFlatXMLTemplate = 21 Const wdFormatFlatXMLTemplateMacroEnabled = 22 Const wdFormatHTML = 8 Const wdFormatPDF = 17 Const wdFormatRTF = 6 Const wdFormatTemplate = 1 Const wdFormatTemplate97 = 1 Const wdFormatText = 2 Const wdFormatTextLineBreaks = 3 Const wdFormatUnicodeText = 7 Const wdFormatWebArchive = 9 Const wdFormatXML = 11 Const wdFormatXMLDocument = 12 Const wdFormatXMLDocumentMacroEnabled = 13 Const wdFormatXMLTemplate = 14 Const wdFormatXMLTemplateMacroEnabled = 15 Const wdFormatXPS = 18 On Error Resume Next Set objWord = CreateObject( "Word.Application" ) If Err Then Syntax "Unable to access MS Word. Make sure MS Office is installed" & vbCrLf & vbTab & "(MSI based installation, NOT a ""click-to-run"" installation)." Else objWord.Visible = True objWord.Documents.Open myWordFile If Err Then Syntax "Unable to open the specified document in MS Word." Else Set objDoc = objWord.ActiveDocument objDoc.SaveAs myPDFFile, wdFormatPDF objDoc.Close Set objDoc = Nothing If Err Then Syntax "Unable to save the document as PDF." End If objWord.Quit Set objWord = Nothing End If On Error Goto 0 End Sub Sub Syntax( myMessage ) Dim strMsg On Error Resume Next objDoc.Close objWord.Quit Set objDoc = Nothing Set objWord = Nothing Set objFSO = Nothing On Error Goto 0 strMsg = "" If Trim( " " & myMessage ) <> "" Then strMsg = vbCrLf & "ERROR:" & vbTab & myMessage & vbCrLf strMsg = strMsg _ & vbCrLf _ & "Word2PDF.vbs, Version 1.11" _ & vbCrLf _ & "Open a Microsoft Word document and save it in PDF format" _ & vbCrLf & vbCrLf _ & "Usage:" & vbTab & "WORD2PDF.VBS wordfile [ pdffile ] [ /O ]" _ & vbCrLf & vbCrLf _ & "Where:" & vbTab & """wordfile""" & vbTab & "Word document(s) to be converted (wildcard" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "allowed for file name, e.g. ""*.doc"" or ""*.docx"")" _ & vbCrLf _ & " " & vbTab & """pdffile""" & vbTab & "PDF file to be created (no wildcards allowed," _ & vbCrLf _ & " " & vbTab & " " & vbTab & "default: path/name of Word file, extension "".pdf"")" _ & vbCrLf _ & " " & vbTab & "/O " & vbTab & "silently Overwrite existing PDF files (default:" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "abort with error message if PDF file exists)" _ & vbCrLf & vbCrLf _ & "Notes:" & vbTab & "[1]" & vbTab & "This script requires a ""regular"" (MSI based)" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "Microsoft Word installation, it WILL FAIL on" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "a ""click-to-run"" installation of MS Office." _ & vbCrLf _ & " " & vbTab & "[2]" & vbTab & "For Word 2007, this script requires the" _ & vbCrLf _ & " " & vbTab & " " & vbTab & """Microsoft Save as PDF or XPS Add-in for 2007" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "Microsoft Office programs"", available at:" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "www.microsoft.com/en-us/download/details.aspx?id=7" _ & vbCrLf _ & " " & vbTab & "[3]" & vbTab & "Wildcard ""*"" is allowed for the file NAME of the" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "Word document, but in that case no PDF file should" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "be specified, as wildcards are NOT allowed in the" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "PDF file specification." _ & vbCrLf _ & " " & vbTab & "[4]" & vbTab & "If wildcard ""*"" is used for the Word document, and" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "the /O switch is not used, the script will display an" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "error message in case a PDF file already exists, but" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "it will then continue to convert the next file instead" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "of aborting." _ & vbCrLf _ & " " & vbTab & "[5]" & vbTab & "If Word was already active when this script is started," _ & vbCrLf _ & " " & vbTab & " " & vbTab & "the other document(s) will be left alone, and only the" _ & vbCrLf _ & " " & vbTab & " " & vbTab & "document opened by this script will be closed." _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" _ & vbCrLf _ & "http://www.robvanderwoude.com" WScript.Echo strMsg WScript.Quit 1 End Sub