Print a PDF file with shell command in VBA

mrlionsss

New Member
Joined
Nov 11, 2020
Messages
10
Office Version
  1. 2007
Platform
  1. Windows
I am using this command in Excel VBA to print an existing PDF file as PDF

VBA Code:
Shell """" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" _
          & """/p /h """ & ActiveWorkbook.Path & "\" & Range("A8") & "invoice.pdf" & """"

This works great, however I would like to be able to overwrite the existing file. It obviously gives me an error message, that the file is being used, therefore I cannot overwrite it.

Is there a way to do this?

If not, is there a way to set a different predefined file name for the printed file? E.g. the file that needs to be printed is Range("A8") & "invoice.pdf" and the printed PDF would suggest Range("A8") & "invoice_printed.pdf"

Any help is appreciated!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try the following code:
Dim oShell As Object, oExec As Object
Set oShell = CreateObject("Wscript.Shell")
Set oExec = oShell.exec("""" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" _
& """/p /h """ & ActiveWorkbook.Path & "\" & Range("A8") & "invoice.pdf" & """")
DoEvents
oExec.Terminate
Set oExec = Nothing
Set oShell = Nothing

Regards,
 
Upvote 0
Solution

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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