VBA - Command Button to print current worksheet + attached PDF

dmanso

New Member
Joined
Jul 18, 2022
Messages
9
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hi there,

I have been searching for a solution to this for quite some time. I have a workbook with multiple worksheets. On one of the worksheets, I have attached PDFs as objects. Within the same worksheet, I am hoping to create a command button which will print when clicked on, the current worksheet and the attached PDFs. My intention is to use Microsoft print to PDF function, and "merge" the worksheet together with the PDFs.

My apologies for my vague explanation. I am also extremely new to VBA, and will really appreciate any assistance.

Thank you again
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Very much appreciated! I will definitely have a good read. I think I may have come across this earlier on, but due to Adobe Pro, which I do not have, it was not feasible. I wonder if I can get it to work using a opensource PDF merger. Thank you again
 
Upvote 0
Alternatively, there are some command line tools which can merge PDFs from VBA, for example PDFtk Server and PDFill. Example code here:

 
Upvote 0
Hi there John_w, thank you again for the help. I have played around with the codes, and unfortunately as I do not have Adobe Pro, I cannot get it to work.

I am wondering if it is possible to use a button (form or a shape) to print the active worksheet and the embedded PDFs, and merging them using PDFSam (opensource)? I cannot for the life of me figure out how to update the Macro from Adobe to PDFSam... Any help will be much appreciated!
 
Upvote 0
Does PDFsam have a command line interface? I couldn't find any documentation or examples.
 
Upvote 0
Thanks John_w. I managed to find something another MrExcel member had posted (Kenneth Hobson) and he wrote a macro as below (although this is for splitting rather than merging?) I cannot for the life of me understand it or convert it to merge. My PDFSam version is 4.3.1:

Macro from Kenneth

'How to split a PDF into pages using VBA

Sub Test2()
Dim pdfsam As String, pdfFiles As String, pdfsamStr As String
Dim pdfOut As String, q As String
q = """"
pdfsam = "cmd /k java -jar " & q & _
"C:\MyFiles\pdfsam\2.0.0\lib\" & _
"pdfsam-console-2.0.2e.jar" & q
pdfFiles = "-f " & q & "C:\MyFiles\pdfsam\2.0.0\doc\pdfsam-1.0.0-tutorial.pdf" & q
pdfOut = "-o " & q & Environ("temp") & "\" & q
pdfsamStr = pdfsam & " -p SamDoc -s BURST " & pdfFiles & " " & pdfOut & " SPLIT"
Debug.Print pdfsamStr
Shell pdfsamStr, vbNormalFocus
End Sub

Sub Test1()
Dim pdfsam As String, pdfFiles As String, pdfsamStr As String
Dim pdfOut As String, q As String
q = """"
pdfsam = "cmd /k java -jar " & q & _
"C:\MyFiles\pdfsam\1.0.3\lib\" & _
"pdfsam-console-1.1.5e.jar" & q
pdfFiles = "-f " & q & "C:\MyFiles\pdfsam\1.0.3\doc\pdfsam-1.0.0-tutorial.pdf" & q
pdfOut = "-o " & q & "c:\temp" & "\" & q
pdfsamStr = pdfsam & " " & pdfFiles & " -p ken.pdf -s BURST " & pdfOut & " split"
Debug.Print pdfsamStr
Shell pdfsamStr, vbNormalFocus
End Sub

Sub TestBat()
Dim pdfsam As String, pdfFiles As String, pdfsamStr As String
Dim pdfOut As String, q As String
q = ""
pdfsam = "cmd /k java -jar " & q & _
"C:\MyFiles\pdfsam\1.0.3\bin\" & _
"run-console.bat" & q
pdfFiles = "-f " & q & "C:\MyFiles\pdfsam\1.0.3\doc\pdfsam-1.0.0-tutorial.pdf" & q
pdfOut = "-o " & q & "c:\temp" & "\" & q
pdfsamStr = pdfsam & " " & pdfFiles & " " & pdfOut & " -p ken.pdf -s BURST split"
Debug.Print pdfsamStr
Shell pdfsamStr, vbNormalFocus
End Sub
 
Upvote 0
I must admit I do not know for sure as I have not tested them.
 
Upvote 0
Wondering if anyone can help? If it is not possible without Adobe Pro. Is it possible to simply print the active sheet, and the embedded PDF, using the "Microsoft print to PDF" as separate files (into the same folder)? Sorry, I am quite useless with VBA and will need all the help I can get. Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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