Excel VBA to open a PDF and print as PDF

mrlionsss

New Member
Joined
Nov 11, 2020
Messages
10
Office Version
  1. 2007
Platform
  1. Windows
I'm using this code that I found in another thread. It opens a pdf file in Adobe Reader and prints it to a PDF. It works great, however, when I run the code, it triggers a dialog window in Adobe Acrobat, where you are suppose to enter the file name and hit save. I would need it to have the file name already set when printing and skip the part with the dialog window, in another way, is it possible, that all of this happens in the background?

Unfortunately, my vba knowledge doesn't go that far, so any help is appreciated!

VBA Code:
Option Explicit
Public 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 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
Dim prtFile As String
strDir = "C:\Users\admin\Desktop\test"
strFile = "filetoprint.pdf"
 
printThis = PrintThisDoc(0, strDir & "\" & strFile)

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi mrlionsss
Answer found by oswaler
oswaler is offline

Go to the list of printers in the Windows Settings list and right click the Adobe PDF icon and click on Properties. On the Advanced tab click on the Printing Defaults button. There is a setting there for Adobe pdf output folder. By default this is set to prompt the user. If you click the browse button you can set a folder for it to save to. There is also a checkbox you can use to turn off the autoloading of the document after it is created.
 
Upvote 0
If no browse button to add folder, please click printer property. Go to tab "Ports" and add port instead. After done, new folder will be available to set as output folder.
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,533
Members
452,652
Latest member
eduedu

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