Word VBA, Print as PDF

In this article I will explain how you can use VBA for word to save a document as a pdf.


Basics:

The basic code for saving a word document as a pdf can be seen below:

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"D:StuffBusinessTempPDFName.pdf", _
ExportFormat:=wdExportFormatPDF

The code above saves the entire document at the path “D:StuffBusinessTemp” under the name “PDFName.pdf”.


Print Certain Pages:

Using the code below you will be able to save a certain range of pages:

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"D:StuffBusinessTempPDFName.pdf", _
ExportFormat:=wdExportFormatPDF, _
Range:=wdExportFromTo, From:=1, To:=1

Note that word pages start from the index “1”.


Select Save Path:

Using a save file dialog you can select the path and filename to save the pdf file during runtime:

Sub Example3()
Dim intChoice As Integer
Dim strPath As String
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = _
Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
'print the file as pdf
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"D:StuffBusinessTempPDFName.pdf", _
ExportFormat:=wdExportFormatPDF, _
Range:=wdExportFromTo, From:=1, To:=1
End If
End Sub

The code above will display a save file dialog asking the user to select the path and name to save the file:

Print Save File Dialog
You can download the file and code related to this article from the link below:

 

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com

 

One thought on “Word VBA, Print as PDF”

  1. Markos says:

    In your last example

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    “D:StuffBusinessTempPDFName.pdf”,

    should be:

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    strPath,

Leave a Reply

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

privacy policy