Friday, March 28, 2014

Batch convert Word documents to .pdf files in Word 2007 and later

This is brilliant. "Faster" posted some vbs macro code in 2011 which uses Word 2007 and later to convert all files in a folder to .pdf files (or other formats, even on earlier versions of Word).

A few cautions: the original files are not modified, but the generated .pdf files are put in a sibling folder to the one you select. For instance, if you provide the path c:\docs\wordfiles, the pdfs will be created in c:\docs\wordfilesconverted. Also, make sure there is nothing but Word files in the folder you select, because it will try to convert any files it finds in the given path. Fortunately, it doesn't recurse into subfolders, so you can dump non-word documents into a temporary subfolder before you run the conversion.

For posterity, here is the code:
Option Explicit

Sub ChangeDocsToTxtOrRTFOrHTML()
'with export to PDF in Word 2007
    Dim fs As Object
    Dim oFolder As Object
    Dim tFolder As Object
    Dim oFile As Object
    Dim strDocName As String
    Dim intPos As Integer
    Dim locFolder As String
    Dim fileType As String
    On Error Resume Next
    locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\myDocs")
    Select Case Application.Version
        Case Is < 12
            Do
                fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT"))
            Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML")
        Case Is >= 12
            Do
                fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT"))
            Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF")
    End Select
    Application.ScreenUpdating = False
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fs.GetFolder(locFolder)
    Set tFolder = fs.CreateFolder(locFolder & "Converted")
    Set tFolder = fs.GetFolder(locFolder & "Converted")
    For Each oFile In oFolder.Files
        Dim d As Document
        Set d = Application.Documents.Open(oFile.Path)
        strDocName = ActiveDocument.Name
        intPos = InStrRev(strDocName, ".")
        strDocName = Left(strDocName, intPos - 1)
        ChangeFileOpenDirectory tFolder
        Select Case fileType
        Case Is = "TXT"
            strDocName = strDocName & ".txt"
            ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
        Case Is = "RTF"
            strDocName = strDocName & ".rtf"
            ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
        Case Is = "HTML"
            strDocName = strDocName & ".html"
            ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
        Case Is = "PDF"
            strDocName = strDocName & ".pdf"

            ' *** Word 2007 users - remove the apostrophe at the start of the next line ***
            'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
           
        End Select
        d.Close
        ChangeFileOpenDirectory oFolder
    Next oFile
    Application.ScreenUpdating = True
End Sub
Activate the Developer tab in Word, then open up the vbs console (or create a macro) and paste this code in (make sure the lines don't wordwrap like they do on this page), then run it. Assuming you're running Word 2007 or later, be sure to delete the apostrophe where indicated.

For more details, check out the original post.

While we're on the subject, may I further recommend PDFbinder as a free program which allows you to merge many pdf files into a single multi-page file. And also Compress PDF, a free online service which compresses your .pdf file sizes.

~Till next time!

Labels: ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home