Export Word to PDF – with VBA – Convert Docx to PDF

Convert MS Word Docx file to PDF

To manually save or export a word document to PDF, the option is available in File menu itself.

  1. Click ‘File’ menu
  2. Choose ‘Save As’
  3. Type any file name
  4. In save as type option, select “PDF (*.pdf)”
  5. Click on Save

This will convert all the page in the docx file to PDF. This option is available only in the higher version of office like 2010, 2013, 2016 etc., Other possible manual methods are discussed in this article.

Now, lets see how to do this a Vba code.

VBA Code to Export Word to PDF

This vba code is written to run as Word Vba code.

In case if you would like to automate this process from Excel or Outlook, then an object for word.application has to be created first. Then the export function if word can be accessed using that object.

Sub ConvertWordToPDF()
    'Code from Officetricks
    'Define variable fields
    Dim sFolderPath As String
    Dim sFileName As String
    Dim sNewFileName As String
    
    'Check if Folder Exists
    sFolderPath = ActiveDocument.Path & "\"
    
    'Set New File Name
    sFileName = ActiveDocument.Name
    sNewFileName = VBA.Mid(sFileName, 1, VBA.InStrRev(sFileName, ".", , vbTextCompare) - 1)
    sNewFileName = sFolderPath & sNewFileName & ".pdf"
    
    'Save File
    ActiveDocument.ExportAsFixedFormat OutputFileName:=sNewFileName, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub

Run the code by pressing F5. It will export the current Word document to a PDF file with the new name given in the code.

Related topic: Convert Excel to PDF

We could use this conversion from word to pdf to save the intermediate work we do while editing a word document or while reviewing for any quality.

Also, the converted Pdf can be shared with others, so that it is not altered by others.

External Reference: Discussion on saving Word Doc to PDF