Windows 7 batch command-line to save as .pdf file for word 2013 .docx file

9

4

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?

Nam G VU

Posted 2014-07-30T03:20:44.140

Reputation: 10 255

1

Check out this question: http://superuser.com/questions/541357/add-right-click-save-as-pdf-in-windows-explorer

– None – 2014-07-30T03:56:31.760

Answers

10

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

Oleksiy Kovtun

Posted 2014-07-30T03:20:44.140

Reputation: 181

0

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.

Al Johri

Posted 2014-07-30T03:20:44.140

Reputation: 156