Created
September 6, 2017 14:44
-
-
Save ciis0/20bed0584ce83b2aea616ab4719d614b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cscript alldocx2pdf.vbs %1 | |
pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Convert all .docx in a folder to PDF. | |
' Mar, 2017, Christoph Schulz <christoph.2.schulz@atos.net> | |
' GPLv3 or later | |
' Based on script by BillP3rd | |
' https://superuser.com/a/641661 | |
' Usage: alldocx2pdf [folder] | |
' folder: folder containing the .docx-s. | |
' The PDF file name will be the same as the .docx, but .pdf. (e.g. document.docx becomes document.pdf). | |
' Currently exports only the first page, see line with doc.ExportAsFixedFormat (L86) | |
' PDF only will be created if it does not already exists. | |
Option Explicit | |
Sub WriteLine ( strLine ) | |
WScript.Stdout.WriteLine strLine | |
End Sub | |
' Declarations for Word | |
Const wdExportOptimizeForOnScreen = 1 | |
Const wdExportOptimizeForPrint = 0 | |
Const wdExportFormatPDF = 17 | |
Const wdExportFormatXPS = 18 | |
Const wdExportDocumentContent = 0 | |
Const wdExportDocumentWithMarkup = 7 | |
Const wdExportCreateHeadingBookmarks = 1 | |
Const wdExportCreateNoBookmarks = 0 | |
Const wdExportCreateWordBookmarks = 2 | |
Const wdExportAllDocument = 0 | |
Const wdExportCurrentPage = 2 | |
Const wdExportFromTo = 3 | |
Const wdExportSelection = 1 | |
Const wdDoNotSaveChanges = 0 | |
Const wdPromptToSaveChanges = -2 | |
Const wdSaveChanges = -1 | |
' Objects | |
Dim objFso | |
Dim objWord ' Word instance | |
' Other variables | |
Dim folder, files, file, output, folderFile, doc | |
Set objFso = CreateObject("Scripting.FileSystemObject") | |
WriteLine "Starting Word..." | |
Set objWord = CreateObject( "Word.Application" ) | |
WriteLine "Started." | |
' Open folder and get files | |
Set folder = objFso.GetFolder(WScript.Arguments(0)) | |
Set files = folder.Files | |
WriteLine "folder: " & folder | |
' Open the .docx's | |
For each folderFile In files | |
file = folder & "\" & folderFile.Name ' make "full" path | |
output = replace(file, ".docx", ".pdf") | |
' Open only .docx, but not backup ($~...) files and only if pdf does not already exists | |
If instr(file, ".docx") > 0 And instr(file, "~$") = 0 And Not objFso.FileExists(output) Then | |
WriteLine "Opening " & file & "..." | |
objWord.Documents.Open file | |
End If | |
Next | |
' Create PDFs | |
For Each doc In objWord.Documents | |
file = doc.fullName | |
output = Replace(file, ".docx", ".pdf") | |
WriteLine "In : " & file | |
WriteLine "Out: " & output | |
' ExportAsFixedFormat(OutputFileName, ExportFormat, OpenAfterExport, OptimizeFor, ExportCurrentPage) | |
doc.ExportAsFixedFormat output, wdExportFormatPDF, False, wdExportOptimizeForOnScreen, wdExportCurrentPage | |
doc.Close wdDoNotSaveChanges | |
Next | |
' Quit Word | |
objWord.Quit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment