• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Add to excel VBA that prints pdf file

bnpdkh

Member
I found the code below online and use it to print a pdf file. This work perfectly with no issues. I would like to add the following if possible;
1. orientation=landscape
2. Page size = 11x17
3. Print only the first three pages
4. Print 2 copies.
This is beyond my ability in VBA, I just copied the code and changed the file location and name information. Any help would be appreciated.

Code:
Option Explicit
Public 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
 
Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function
 
Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "H:\Planning\Online Milestone"
strFile = "Milestone Indicators Dashboard.pdf"
 
printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub
 
The changes to the code below will fix the Number of Copies

For the Page size and orientation I would setup a new Printer called MyPrinter with these settings as the default I have changed the command to printy to a printer called MyPrinter

Code:
Public Function PrintThisDoc(formname As Long, FileName As String, Optional Copies As Integer = 1)
On Error Resume Next
Dim X As Long
Dim i As Integer
For i = 1 To Copies
X = ShellExecute(formname, "Print /D:MyPrinter", FileName, 0&, 0&, 3)
Next i
End Function

Code:
Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
Dim Copies As Integer
strDir = "H:\Planning\Online Milestone"
strFile = "Milestone Indicators Dashboard.pdf"
Copies = 2

printThis = PrintThisDoc(0, strDir & "\" & strFile, Copies)
End Sub

No idea about just the first 3 pages
 
Thanks Hui, will give this a try. No worries regarding printing only the first three pages, not a show stopper by any means.
 
Hello...thank you Hui and bnpdkh fro the great post; however, being able to determine a page range for the print job is very important and save tons of papers for me (and environment of course). Could anyone help to add "page range" option in the code. Thanks!
 
Back
Top