This code example will show you how to automate PDF printing from a Microsoft Excel macro. The prerequisite for this code example is that 7-PDF Printer has been defined as default printer!

You will see how you can locate the correct printer name and use the COM interface to automate the printer settings. It only shows you the basics, but you can build your own functionality using this example as an inspiration. This examples works on both 32 and 64 bit Windows.

The fairly old version of Microsoft Office 2003 might experience a runtime error while executing the code sample. If this is the case, Microsoft has released a patch for Office 2003. Further information incl. Patchlink can be found here...

With newer Microsoft Office versions such as Office 365 or Excel 365 etc. there should no longer be any runtime errors, the only important thing is that the 7-PDF Printer has been made the standard printer beforehand!

Option Explicit

Sub PrintSheetAsPDF()
    PrintSheet
End Sub

Sub PrintSheet(Optional sFileName As String = "", Optional confirmOverwrite As Boolean = True)
    Dim oPrinterSettings As Object
    Dim oPrinterUtil As Object
    Dim sFolder As String
    Dim sCurrentPrinter As String
    Dim xmldom As Object
    Dim sProgId As String
    Dim sPrintername As String
    Dim sFullPrinterName As String
    
    Rem -- ***************************************************************************
    Rem -- IMPORTANT: 7-PDF Printer has to be the default printer for the example to work
    Rem -- ***************************************************************************
    
    Rem -- Documentation of the used COM interface is available at the link below.
    Rem -- https://www.7-pdf.de/sites/default/files/guide/dotnet/chm/html/T_pdf7_PdfWriter_PdfSettings.htm
    
    Rem -- Create the objects to control the printer settings.
    
    Set oPrinterSettings = CreateObject("pdf7.PdfSettings")
    Set oPrinterUtil = CreateObject("pdf7.PdfUtil")
    
    Rem -- Get default printer name
    sPrintername = oPrinterUtil.DefaultPrintername
    oPrinterSettings.Printername = sPrintername
        
    Rem -- Prompt the user for a file name
    sFolder = Environ("USERPROFILE") & "\Desktop\"
    If sFileName = "" Then
        sFileName = InputBox("Save PDF to desktop as:", "Sheet '" & _
            ActiveSheet.Name & "' to PDF...", ActiveSheet.Name)
                
        Rem -- Abort the process if the user cancels the dialog
        If sFileName = "" Then Exit Sub
        
        sFileName = sFolder & sFileName
    End If
        
    Rem -- Make sure that the file name ends with .pdf
    If LCase(Right(sFileName, 4)) <> ".pdf" Then
        sFileName = sFileName & ".pdf"
    End If
    
    Rem -- Write the settings to the printer
    Rem -- Settings are written to the runonce.ini
    Rem -- This file is deleted immediately after being used.
    
    With oPrinterSettings
        .SetValue "Output", sFileName
        If confirmOverwrite Then
            .SetValue "ConfirmOverwrite", "yes"
        Else
            .SetValue "ConfirmOverwrite", "no"
        End If
        .SetValue "ShowSettings", "never"
        .SetValue "ShowPDF", "yes"
        .WriteSettings True
    End With
    
    Rem -- Print the active work sheet
    ActiveSheet.PrintOut
    
End Sub

Downloads

Attachment Size
Example file 16.24 KB

Top