Go to Top

How to create PDFs in Word using Access VBA

Office 2007 will allow you to easily create PDFs from any Word Document using the output menu on the Office button, (Access does too with Reports), but recently I had to create a procedure for a client that wanted to convert Word docs to PDF, email them and do it all from Access.

Note: To make this code work, you will need to add a reference to Word 2007 to your Access database. 

ADVERTISING
ACCESS SAFETY AND TRAINING DATABASE

Here is the code:
Private Sub CreatePDF(strSourceFile As String, strDestFile As String)
    Dim objWord As Word.Application
    Dim objWordDoc As Word.Document
   
    On Error GoTo ErrorHandler
   
    Set objWord = CreateObject(“Word.Application”)
    objWord.Visible = True
    Set objWordDoc = objWord.Documents.Open(strSourceFile)
    If Not objWord Is Nothing Then
        objWordDoc.ExportAsFixedFormat strDestFile, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument
    End If
   
ExitProcedure:
    objWordDoc.Close False
    objWord.Quit
    Set objWordDoc = Nothing
    Set objWord = Nothing
    Exit Sub
   
ErrorHandler:
    MsgBox Err.Description, vbInformation, “Error Creating PDF”
   
End Sub

Note how I display Word at the begining of the process, not doing so may leave Word “hanging” in memory if your code comes back with an error.

About Juan Soto

Juan Soto is a Senior Access Developer at IT Impact Inc. and a Microsoft Access MVP. He specializes in Access with SQL Server databases. His passion for Access has led him to helping a wide range of businesses in helping them establish a secure, stable and efficient environment with SQL Server. He's a frequent speaker at Access user groups nationwide and recently spoke at the Orange County SQL Saturday # 73. If you wish to have Juan speak at your next group meeting you can contact him here.

One Response to "How to create PDFs in Word using Access VBA"

  • John Martin
    April 14, 2015 - 6:32 pm Reply

    It doesn’t require any tool or software to convert word file in to PDF. Just save as your document to PDF and you just done it. But yest if you want to print it, you may need a tool or software. I use FolderMill software trial version downloaded from http://www.foldermill.com/

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Contact Us
[gravityform id="16" title="false" description="false"]