Author: Pliman
This is a tool written using VBScript and batch files to batch convert Microsoft Word documents (.doc and .docx) in a specified folder to PDF files.
The tool offers two modes of operation:
- Convert Individually: Converts each Word document in the folder to a corresponding PDF file separately.
- Merge then Convert: First, all Word documents in the folder are merged into a single Word document in alphabetical order of their full file paths. Then, this merged document is converted into a PDF file.
- Requirements:
- Windows Operating System.
- Microsoft Word installed.
- Files: Ensure the following three files are in the same directory:
ConvertAllWordToPDF.bat(Main execution script)WordToPDF.vbs(Script for converting individual files)MergeWordDocs.vbs(Script for merging multiple files)
- Execution:
- Double-click
ConvertAllWordToPDF.batto run it. - The script will prompt you to enter the full path of the folder containing the Word documents.
- After entering a valid path, the script will ask you to choose an operation mode (enter
1for individual conversion,2for merge then convert). - The script will perform the corresponding action based on your selection.
- Double-click
- Output:
- Convert Individually: The generated PDF files will be located in the same folder as the original Word documents and will have the same filenames (with a
.pdfextension). - Merge then Convert:
- A merged Word document named
_Merged_Documents_YYYYMMDD_HHMMSS.docxwill be created (where YYYYMMDD_HHMMSS is the timestamp). - A final PDF file named
_Merged_Documents_YYYYMMDD_HHMMSS.pdfwill be generated. - Both files are saved in the folder you specified.
- A merged Word document named
- Convert Individually: The generated PDF files will be located in the same folder as the original Word documents and will have the same filenames (with a
- Log: The script will output the progress and results (success or failure and error codes) of the conversion/merging process in the console during runtime.
- Batch Script (
.bat):- Serves as the user interaction entry point, obtaining the target folder path and user operation choice.
- Calls
chcp 65001to attempt to set the console code page to UTF-8, to support Chinese paths or filenames if present. - Executes VBScripts using the
cscript //NoLogo <VBScript_Path> [Arguments]command. - Checks for the existence of VBScripts and the validity of the target folder.
- Determines the success of operations based on the
ERRORLEVELreturned by the VBScripts and outputs corresponding messages. - Generates timestamped filenames in merge mode to avoid conflicts.
- VBScript (
.vbs):- Utilizes
CreateObject("Word.Application")to create a Microsoft Word COM automation object to programmatically control Word. WordToPDF.vbs:- Accepts the input Word file path and an optional output PDF file path as arguments.
- Uses the
Word.Applicationobject'sDocuments.Openmethod to open Word documents. - Uses the
Document.SaveAs2method, specifying thewdFormatPDF(constant value 17) parameter, to save the document as a PDF. - Includes error handling (
On Error Resume Next,Err.Number) to catch potential issues during Word operations (e.g., file not found, cannot open, cannot save). - Returns an execution status code to the batch script via
WScript.Quit(ErrorCode).
MergeWordDocs.vbs:- Accepts the input folder path and the output merged Word file path as arguments.
- Uses
CreateObject("Scripting.FileSystemObject")to iterate through files in the specified folder. - Uses
CreateObject("System.Collections.ArrayList")to store the paths of found Word files and sorts them alphabetically by full path. - Creates a new Word document (
Word.Application.Documents.Add) as the merge target. - Iterates through the sorted list of files, using the
TargetDocument.Range.InsertFilemethod to insert the content of each document at the end of the target document. A page break (wdSectionBreakNextPage) is inserted before inserting non-first files. - Finally, saves the merged document in
.docxformat (wdFormatDocumentDefault). - Also includes error handling and status code return mechanisms.
- Both VBScripts ensure Word runs silently in the background by setting
Word.Application.Visible = FalseandWord.Application.DisplayAlerts = False.
- Utilizes