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, …
Thank you so much for this. I have tried lots of solutions from web and none of the early binding approaches would work for me, kept kicking up errors, that I couldn’t solve. I am very grateful that you have shared your solution here.
Thank you very much for your great codes. I tried many other VBA codes in Excel to convert a word document to pdf fiel but they didn’t work. There were some errors always till I see your codes. It works well immediately without problems. Thanks.
Thank you for the feedback. I always appreciate knowing that my code is helpful.
Dear Mr Pineault,
I want to tag along respondents to your code, because that is exactly what happened to me: neither the forum I frequently visit nor searches through innumerable pieces of code produced what I needed. Your code above did at once, no issues, immediately… to my immense relief!
Thank you times a million.
Hi
Thanks for your codes. It works very well.
I have one question: oDoc.ExportAsFixedFormat sSavePath & sFileName & “.pdf”, 17
What does 17 mean?
If you look at the official documentation for the Document.ExportAsFixedFormat method, specifically the ExportFormat input variable it uses WdExportFormat enumeration. Since we are using Late binding, I simply used the value, 17, associated with wdExportFormatPDF. We could just as easily declare Const wdExportFormatPDF = 17 as the use the variable in the code.
Great bit of code – thanks