Skip to content

Pliman/batch-convert-word2pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

中文文档

Batch Word to PDF Tool

Author: Pliman

Introduction

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:

  1. Convert Individually: Converts each Word document in the folder to a corresponding PDF file separately.
  2. 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.

Usage / Interface

  1. Requirements:
    • Windows Operating System.
    • Microsoft Word installed.
  2. 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)
  3. Execution:
    • Double-click ConvertAllWordToPDF.bat to 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 1 for individual conversion, 2 for merge then convert).
    • The script will perform the corresponding action based on your selection.
  4. 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 .pdf extension).
    • Merge then Convert:
      • A merged Word document named _Merged_Documents_YYYYMMDD_HHMMSS.docx will be created (where YYYYMMDD_HHMMSS is the timestamp).
      • A final PDF file named _Merged_Documents_YYYYMMDD_HHMMSS.pdf will be generated.
      • Both files are saved in the folder you specified.
  5. 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.

Implementation Principles

  • Batch Script (.bat):
    • Serves as the user interaction entry point, obtaining the target folder path and user operation choice.
    • Calls chcp 65001 to 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 ERRORLEVEL returned 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.Application object's Documents.Open method to open Word documents.
      • Uses the Document.SaveAs2 method, specifying the wdFormatPDF (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.InsertFile method 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 .docx format (wdFormatDocumentDefault).
      • Also includes error handling and status code return mechanisms.
    • Both VBScripts ensure Word runs silently in the background by setting Word.Application.Visible = False and Word.Application.DisplayAlerts = False.

License

MIT License

About

convert Microsoft Word documents (`.doc` and `.docx`) in a specified folder to PDF files

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published