BaturFurkan
New Member
- Joined
- Feb 4, 2021
- Messages
- 4
- Office Version
- 2010
- Platform
- 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.
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