0

After many days searching to print multiples pages of a pdf file in a single sheet by using only VBA code on Excel I find a way to use javascript via VBA thanks to user ReFran https://stackoverflow.com/a/49538950/16279586

Combining the code ReFran wrote with this reference https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/pdfs/acrobatsdk_jsapiref.pdf#G4.1959527 I build some Frankenstein monster that doesn't work at all.

Suposedly it will open a pdf file, then print 2x2 pages on a single sheet and save in the same document the changes (turning from a 4 pages pdf to a 1 page pdf) I'm a newbie in VBA and absolutely ignorant in javascript, can anyone give me a helping hand with this?

Sub test()
Dim app As acroApp
Dim avdoc As AcroAVDoc
Dim aform As AFormApp


Path = "C:\1.pdf"

Set app = CreateObject("Acroexch.app")
app.Show
Set avdoc = CreateObject("AcroExch.AVDoc")
Set aform = CreateObject("AFormAut.App") 'from AFormAPI

If avdoc.Open(Path, "") Then
    '// write some js code on a vbs variable
    
    js = "pp = this.getPrintParams();" _
    & "pp.printerName = "";" _
    & "pp.pageHandling = pp.constants.handling.nUp;" _
    & "pp.nUpPageOrders = pp.constants.nUpPageOrders.Horizontal;" _
    & "pp.nUpNumPagesH = 2;" _
    & "pp.nUpNumPagesV = 2;" _
    & "pp.nUpPageBorder=true;" _
    & "this.print(pp);"
     '//execute the js code
    aform.Fields.ExecuteThisJavaScript js
End If

Set aform = Nothing
Set avdoc = Nothing
Set app = Nothing
End Sub
  • What does it do instead of working? – Tim Williams Jul 07 '21 at 22:35
  • Thank you @TimWilliams and @K J I have not received any alert window on VBA however when I open the pdf (the one on the test or another one it shows the following sentence in an javascript debugger window SyntaxError: missing ; before statement 1:External:Exec I have an acrobat DC Pro license so there shouldn't be any issue with licenses – carlos_ruiz Jul 08 '21 at 06:18
  • Try `& "pp.printerName = """";" _` (doubled-up double quotes) or `& "pp.printerName = '';" _` (pair of single quotes) – Tim Williams Jul 08 '21 at 17:06
  • Dear @TimWilliams now it shows the print window, but it gets stuck on that point. Eventually an alert window pops on excel telling that excel is waiting for the pdf file to complete an OLE action. If I click on accept or "change to" the window pop up again eventually in few seconds. If I click on cancel then it shows me the following message (translate from spanish) an error happen '-2147418110 (800100002)' in execution time Error on the 'ExecuteThisJavascript' 'IFields' object method Thank you in advance – carlos_ruiz Jul 08 '21 at 20:53
  • I'm not familiar with this area, but you set the printer to an empty string, so I can only guess it's going to prompt you to select a printer? – Tim Williams Jul 08 '21 at 21:48
  • @TimWilliams and @K J No changes in the macro behaviour no matter the printer I'm trying to use, it get stuck allways on the printer window. Since excel pops the error message regarding the 'ExecuteThisJavascript' 'IFields' object method could it be possible that there is something wrong with this line? aform.Fields.ExecuteThisJavaScript js – carlos_ruiz Jul 10 '21 at 10:02

0 Answers0