How to Print a PDF in Excel VBA?

BaturFurkan

New Member
Joined
Feb 4, 2021
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
I've been working on being able to print a PDF file from Excel VBA but having a rough time with it. Later, I came across the codes of the person named @starl on this link (How to Print a PDF in Excel VBA?) . It has greatly relieved my job but I still need to work on it and need your help :(
What I want is to find the Pdf location I typed in cell A1, the Pdf name I typed in B1 and the page I typed in Cell C1 and print two copies. And closing the relevant pdf file at the end of the process.
I've added the relevant code below. The code works fine. I am just having trouble printing the related pdf page in cell c1 and closing pdf at the end of the process.

Code:
Declare PtrSafe 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 PrintPDF(xlHwnd As Long, FileName As String) As Boolean
Dim X As Long

On Error Resume Next
X = ShellExecute(xlHwnd, "Print", FileName, 0&, 0&, 3)

If Err.Number > 0 Then
    MsgBox Err.Number & ": " & Err.Description
    PrintPDF = False
Else
    PrintPDF = True
End If
On Error GoTo 0
End Function
 
Sub PrintSpecificPDF()

Dim strPth As String, strFile As String, strPage As String

strPth = Range("A1")
strFile = Range("B1")
strPage = Range("C1") 'I added this part
 
If Not PrintPDF(0, strPth & strFile) Then
    MsgBox "Printing failed"
End If

End Sub
 

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Watch MrExcel Video

Forum statistics

Threads
1,127,217
Messages
5,623,459
Members
415,971
Latest member
Graeme M

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top