VBA Code to print an existing pdf file (1 Viewer)

atrium

Registered User.
Local time
Today, 14:09
Joined
May 13, 2014
Messages
348
I have saved pdf files and I need to use vba to print them,

I have been able to do it for word and Excel but I haven't found any code to do it for PDF's

Can anyone help please

Atrium
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:09
Joined
May 7, 2009
Messages
19,245
put this code in a Separate module.
to call it:

PrintAnyDocument "AnyPath\yourPDF.pdf"
Code:
Option Compare Database
Option Explicit


'Written by Andrew Fergus
'set a reference to Windows Script Host Object Model
'
' agpuzon
' added x64 support
'
#If Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

Private Declare PtrSafe Function ShellExecute Lib _
    "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As LongPtr, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As LongPtr
#Else
Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

Private Declare Function ShellExecute Lib _
    "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As Long
#End If
Const SW_SHOWNORMAL = 1

Public Sub PrintAnyDocument(strPathAndFileNamePlusExtension As String)

Dim dfltPrinter As String
Dim newPrinter As Object

    ' Create a new WshNetwork object to access network properties.
    Set newPrinter = CreateObject("WScript.Network")
    'Get the name of the default printer
    dfltPrinter = Printer.DeviceName

    'Set the default to the new printer
    'USE YOUR ACTUAL PRINTER NAME BELOW
    'newPrinter.SetDefaultPrinter ("New Printer Name")

    'Pause
    Sleep 100

    'Print the pdf document
    'USE YOUR ACTUAL DOCUMENT NAME & PATH BELOW
    Call ShellExecute(0, "print", strPathAndFileNamePlusExtension, "", 0, SW_SHOWNORMAL)

    'Pause
    Sleep 2000 'wait for 2 seconds - increase this if the code doesn't appear to work

    'Pause
    Sleep 100

    'Set the printer back to the original default
    'newPrinter.SetDefaultPrinter (dfltPrinter)

End Sub
 

cheer

Registered User.
Local time
Today, 12:09
Joined
Oct 30, 2009
Messages
222
put this code in a Separate module.
to call it:

PrintAnyDocument "AnyPath\yourPDF.pdf"
Code:
Option Compare Database
Option Explicit


'Written by Andrew Fergus
'set a reference to Windows Script Host Object Model
'
' agpuzon
' added x64 support
'
#If Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

Private Declare PtrSafe Function ShellExecute Lib _
    "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As LongPtr, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As LongPtr
#Else
Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

Private Declare Function ShellExecute Lib _
    "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Long, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As Long
#End If
Const SW_SHOWNORMAL = 1

Public Sub PrintAnyDocument(strPathAndFileNamePlusExtension As String)

Dim dfltPrinter As String
Dim newPrinter As Object

    ' Create a new WshNetwork object to access network properties.
    Set newPrinter = CreateObject("WScript.Network")
    'Get the name of the default printer
    dfltPrinter = Printer.DeviceName

    'Set the default to the new printer
    'USE YOUR ACTUAL PRINTER NAME BELOW
    'newPrinter.SetDefaultPrinter ("New Printer Name")

    'Pause
    Sleep 100

    'Print the pdf document
    'USE YOUR ACTUAL DOCUMENT NAME & PATH BELOW
    Call ShellExecute(0, "print", strPathAndFileNamePlusExtension, "", 0, SW_SHOWNORMAL)

    'Pause
    Sleep 2000 'wait for 2 seconds - increase this if the code doesn't appear to work

    'Pause
    Sleep 100

    'Set the printer back to the original default
    'newPrinter.SetDefaultPrinter (dfltPrinter)

End Sub

The code on the above will open up the Adobe Reader application. How to execute as hidden at background ?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:09
Joined
May 7, 2009
Messages
19,245
replace the code i gave you with this one.

be warned though that this will open the application(adobe, or word, excel, etc..)
that created this file. i think this one closes the application after
printing the file.

there is no way to print the file without opening the associated application.
if we forced to print it without the association, the printout would be unreadable
(garbaged binary data), except of course for ascii or unicode .txt files.

as before call it with the "Path" + "filename" + "ext":

PrintAnyDocument "D:\SomeFolder\Arnel.pdf"

Code:
Public Sub PrintAnyDocument(ByVal strPathFile As String)
    Dim TargetFolder
    Dim FileName
    Dim ObjShell As Object
    Dim ObjFolder As Object
    Dim ObjItem As Object
    Dim ColItems As Object
    
    If InStrRev(strPathFile, "\") <> 0 Then
        TargetFolder = Left(strPathFile, InStrRev(strPathFile, "\"))
        FileName = Right(strPathFile, Len(strPathFile) - Len(TargetFolder))
    End If
    Set ObjShell = CreateObject("Shell.Application")
    Set ObjFolder = ObjShell.NameSpace(TargetFolder)
    Set ColItems = ObjFolder.Items
    For Each ObjItem In ColItems
        If ObjItem.Name = FileName Then
            ObjItem.InvokeVerbEx ("Print")
            Exit For
        End If
    Next
    Set ObjItem = Nothing
    Set ColItems = Nothing
    Set ObjFolder = Nothing
    Set ObjShell = Nothing
End Sub
 

Users who are viewing this thread

Top Bottom