Tag Archives: MS Word Automation

VBA – Save a Word Document as a PDF

I was asked in a forum how to print/save a word document as a PDF from within Access.

Below is a simple procedure to do exactly that using Late Binding (so no reference libraries are required)

'---------------------------------------------------------------------------------------
' Procedure : PrintWordDoc
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Print/Save a Word document as a PDF in the specified directory
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFile     : Full path, filename and extension of the word document to convert to a PDF
' sSavePath : Path where you would like the PDF saved to
'
' Usage:
' ~~~~~~
' Call PrintWordDoc("C:\Users\Dev\Documents\Test.doc", "C:\Users\Dev\Desktop\")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2016-02-01                  Initial Release
'---------------------------------------------------------------------------------------
Sub PrintWordDoc(sFile As String, sSavePath As String)
    Dim oApp                  As Object
    Dim oDoc                  As Object
    Dim sFileName             As String
    Dim bAppAlreadyOpen       As Boolean

    bAppAlreadyOpen = True
    'Get an instance of word to work with
    'See if Word is already running
    On Error Resume Next
    Set oApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        'Launch a new instance of Word
        Err.Clear
        On Error GoTo Error_Handler
        Set oApp = CreateObject("Word.Application")
        bAppAlreadyOpen = False
    End If
    'Determine the Word doc filename without the path or extension
    sFileName = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))
    sFileName = Left(sFileName, InStr(sFileName, ".") - 1)
    'Ensure the path has the final \
    If Right(sSavePath, 1) <> "\" Then sSavePath = sSavePath & "\"
    'Open the document
    Set oDoc = oApp.Documents.Open(sFile)
    'Print the document as a PDF
    oDoc.ExportAsFixedFormat sSavePath & sFileName & ".pdf", 17

Error_Handler_Exit:
    On Error Resume Next
    'Close the Document
    oDoc.Close False
    Set oDoc = Nothing
    If bAppAlreadyOpen = False Then oApp.Quit
    Set oApp = Nothing
    Exit Sub

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: PrintWordDoc" & vbCrLf & _
           "Error Description: " & Err.Description _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub

This procedure should work in Access, Excel, PowerPoint, …