I'd like to have the fastest way to export my report .docx file to .pdf and distribute it to others whenever I've got a new, updated version. I'm looking for a command-line approach that automates the following steps that I have to do manually using my mouse so far:
File -> Save as -> Browse for location
What are my command options for a batch file?
185 1 1 silver badge 9 9 bronze badges
asked Jul 30, 2014 at 3:20
12.4k 63 63 gold badges 139 139 silver badges 210 210 bronze badges
Check out this question: superuser.com/questions/541357/…
– user181734
Commented Jul 30, 2014 at 3:56
Create a global macro in Word 2013:
' The Word macro for exporting to PDF (the Word window closes after finishing) Sub ExportToPDFext() ChangeFileOpenDirectory ThisDocument.Path ActiveDocument.ExportAsFixedFormat _ OutputFileName:=Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".")) + "pdf", _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ From:=1, _ To:=1, _ Item:=wdExportDocumentContent, _ IncludeDocProps:=True, _ KeepIRM:=True, _ CreateBookmarks:=wdExportCreateNoBookmarks, _ DocStructureTags:=True, _ BitmapMissingFonts:=True, _ UseISO19005_1:=False Application.Quit SaveChanges:=wdDoNotSaveChanges End Sub
After that you can convert a Word document to PDF in command line:
"C:\Program Files\Microsoft Office\Office15\WINWORD.EXE" /mExportToPDFext /q "your_document_path.docx"
The Word window will not even show up because it's set to closing after the macro finishes working, and the parameter /q disables the splash window when Word is loading.
Here are the alternative detailed instructions on GitHub. Also, the context menu option allows batch converting even without the command line. It can be added to registry. For DOC and DOCX:
[HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere] @="Save PDF here" [HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere\command] @="\"C:\\Program Files\\Microsoft Office\\Office15\\WINWORD.EXE\" /mExportToPDFext /q \"%1\"" [HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere] @="Save PDF here" [HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere\command] @="\"C:\\Program Files\\Microsoft Office\\Office15\\WINWORD.EXE\" /mExportToPDFext /q \"%1\""
answered Dec 13, 2014 at 16:47
Oleksiy Kovtun Oleksiy Kovtun
301 3 3 silver badges 4 4 bronze badges
For a simple command-line tool to batch convert you can use, docx2pdf : https://github.com/AlJohri/docx2pdf/
pip install docx2pdf
docx2pdf myFolderOfWordDocs
Disclaimer: I am the author of this tool.
answered Feb 18, 2020 at 7:01 676 7 7 silver badges 5 5 bronze badges Easy to install and use; this tool certainly helped me out. Thanks. Commented Jan 29, 2021 at 8:55Here's the solution from Oleksiy Kovtun adopted to the current Office 16 (Office 360).
In the Word macro I had to change ThisDocument.Path to ActiveDocument.Path .
' The Word macro for exporting to PDF (the Word window closes after finishing) Sub ExportToPDFext() ChangeFileOpenDirectory ActiveDocument.Path ActiveDocument.ExportAsFixedFormat _ OutputFileName:=Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".")) + "pdf", _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, _ From:=1, _ To:=1, _ Item:=wdExportDocumentContent, _ IncludeDocProps:=True, _ KeepIRM:=True, _ CreateBookmarks:=wdExportCreateNoBookmarks, _ DocStructureTags:=True, _ BitmapMissingFonts:=True, _ UseISO19005_1:=False Application.Quit SaveChanges:=wdDoNotSaveChanges End Sub
For the registry Office 16 uses a slightly different paths:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere] @="Save PDF here" [HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere\command] @="\"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE\" /mExportToPDFext /q \"%1\"" [HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere] @="Save PDF here" [HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere\command] @="\"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE\" /mExportToPDFext /q \"%1\""