biopdf logo

Automate PDF Creation from Excel Macros

This code example will show you how to automate PDF printing from a Microsoft Excel macro.

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.

Option Explicit

Sub PrintSheetAsPDF()
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 -- Documentation of the used COM interface is available at the link below.
    Rem --

    Rem -- Create the objects to control the printer settings.
    Rem -- Replace biopdf with bullzip if you have the bullzip printer installed instead
    Rem -- of the biopdf printer.
    Set oPrinterSettings = CreateObject("biopdf.PdfSettings")
    Set oPrinterUtil = CreateObject("biopdf.PdfUtil")

    Rem -- Get default printer name
    sPrintername = oPrinterUtil.DefaultPrintername
    oPrinterSettings.Printername = sPrintername

    Rem -- Get the full name of the printer
    sFullPrinterName = FindPrinter(sPrintername)
    sFullPrinterName = GetFullNetworkPrinterName(sFullPrinterName)

    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"
            .SetValue "ConfirmOverwrite", "no"
        End If
        .SetValue "ShowSettings", "never"
        .SetValue "ShowPDF", "yes"
        .WriteSettings True
    End With

    Rem -- Change to PDF printer
    sCurrentPrinter = ActivePrinter
    ActivePrinter = sFullPrinterName

    Rem -- Print the active work sheet

    Rem -- Restore the printer selection
    ActivePrinter = sCurrentPrinter
End Sub

Function GetFullNetworkPrinterName(NetworkPrinterName As String) As String
    Rem -- Returns the full network printer name
    Rem -- Returns an empty string if the printer is not found
    Rem -- E.g. GetFullNetworkPrinterName("HP LaserJet 8100 Series PCL")
    Rem -- Might return "BIOPDF on Ne04:"
    Dim sCurrentPrinterName As String
    Dim sTempPrinterName As String
    Dim i As Long

    sCurrentPrinterName = Application.ActivePrinter
    i = 0
    Do While i < 100
        sTempPrinterName = NetworkPrinterName & " on Ne" & Format(i, "00") & ":"
        On Error Resume Next
        Rem -- Try to change to the network printer
        Application.ActivePrinter = sTempPrinterName
        On Error GoTo 0
        If Application.ActivePrinter = sTempPrinterName Then
            Rem -- The network printer was found
            GetFullNetworkPrinterName = sTempPrinterName
            Exit Do
        End If
        i = i + 1
    Application.ActivePrinter = sCurrentPrinterName
End Function

Function FindPrinter(sPrinterNameFragment As String) As String
    Rem -- Find the full printer name base on a fragment of the name
    Rem -- Use the GetFullNetworkPrinterName function to get the NeXX
    Rem -- part of the name.
    Dim wsh As Object
    Dim oPrinterCollection
    Dim i As Integer

    Set wsh = CreateObject("WScript.Network.1")
    Set oPrinterCollection = wsh.EnumPrinterConnections
    For i = 1 To oPrinterCollection.Count - 1 Step 2
        If InStr(1, LCase(oPrinterCollection(i)), LCase(sPrinterNameFragment)) > 0 Then
            FindPrinter = oPrinterCollection(i)
            Exit Function
        End If
End Function

Download Example Files

You can download and run the example yourself. The files needed are available here.

Example files (zip archive)

McAfee SiteAdvisor Norton Safe Web