Can't figure out how to print a PDF file using VBA? Can anyone help please????

BrianG86

Board Regular
Joined
Nov 12, 2013
Messages
135
As stated, I cannot figure out how to print off PDF files using VBA.

I have a code that opens a folder and then is supposed to print off all pdf files in that folder.

Can anyone help me out?
 
OK - very important to tell people if you're using 64bit office! ;)

You need something like this:
Code:
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
Const SW_HIDE as long = 0&
Sub PrintPDF(strFile as string)
ShellExecute Application.hWnd, "Print", strFile, 0&, 0&, SW_HIDE
End Sub
 
Upvote 0
Ha I didn't even know it was a 64 bit :stickouttounge:

Ok, so where do I put this part of code then? This is what was on the link with my file paths etc.

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 = "P:\17. PPI Packs\1. BARC001- BARCLAYS\PPIBARC001-002"
strFile = "GTA-000018-009_2013-01-08_0223.pdf"
 
printThis = PrintThisDoc(0, strDir & strFile)
End Sub

Where does it fit in?
 
Upvote 0
The complete code would be:

Code:
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
Const SW_HIDE as long = 0&
Sub PrintPDF(strFile as string)
ShellExecute Application.hWnd, "Print", strFile, 0&, 0&, SW_HIDE
End Sub

Sub testPrint()
Dim strDir As String
Dim strFile As String
 
strDir = "P:\17. PPI Packs\1. BARC001- BARCLAYS\PPIBARC001-002\"
strFile = "GTA-000018-009_2013-01-08_0223.pdf"
 
PrintPDF strDir & strFile
End Sub

Note that I added a backslash to the end of your folder path.
 
Upvote 0
The complete code would be:

Code:
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
Const SW_HIDE as long = 0&
Sub PrintPDF(strFile as string)
ShellExecute Application.hWnd, "Print", strFile, 0&, 0&, SW_HIDE
End Sub

Sub testPrint()
Dim strDir As String
Dim strFile As String
 
strDir = "P:\17. PPI Packs\1. BARC001- BARCLAYS\PPIBARC001-002\"
strFile = "GTA-000018-009_2013-01-08_0223.pdf"
 
PrintPDF strDir & strFile
End Sub

Note that I added a backslash to the end of your folder path.

Rory, thanks for your reply again, I am getting another error message, I have no idea what any of this means to be honest. I though I was getting good at VBA as well :laugh:
Compile Error:
Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules
 
Upvote 0
That code should be in a normal module, not a Worksheet/ThisWorkbook/Userform module. If you need it in such a module for some reason, add the word Private at the start of the 'Declare... ' line.
 
Upvote 0

Forum statistics

Threads
1,226,841
Messages
6,193,291
Members
453,788
Latest member
drcharle

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
Back
Top