11

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?

1

3 Answers 3

12

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\"" 
3

For a simple command-line tool to batch convert you can use, docx2pdf: https://github.com/AlJohri/docx2pdf/

Install:

pip install docx2pdf

Run:

docx2pdf myFolderOfWordDocs

Disclaimer: I am the author of this tool.

1
  • 1
    Easy to install and use; this tool certainly helped me out. Thanks.
    – JustBaron
    Jan 29, 2021 at 8:55
2

Here'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\""

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .