Skip to main content
Known Participant
August 17, 2017
Question

Using excel macro to run JS code in Acrobat Pro

  • August 17, 2017
  • 1 reply
  • 4803 views

I have an interesting question. I have an excel sheet that generates a pretty complicated JS script for Acrobat based on a bunch of user inputs in the spreadsheet. I wrote an excel VBA macro that takes this script, opens the desired PDF, opens the JS console, and pastes the code generated code in the console. However, I can't figure out how to actually run this code once it's in the console. Any ideas? Is it possible to make my VB excel macro force Acrobat to execute a JS?

So far this is what I have. My code is in column 1, anywhere from 10-500 lines long which is why I did the If statement in the loop to remove blank cells. How do I make the console execute the code?

Sub Open_Acrobat()

Dim OpenDoc As String

Dim gApp As AcroApp

Dim gPDDoc As Acrobat.AcroPDDoc

Dim jso As Object

OpenDoc = Application.WorksheetFunction.Concat(Cells(2, 3).Value, "\", Cells(2, 2).Value, ".pdf") 'This line generates the filepath of the doc to open

Set gApp = CreateObject("AcroExch.App")

Set gPDDoc = CreateObject("AcroExch.PDDoc")

 

  If gPDDoc.Open(OpenDoc) Then

    Set jso = gPDDoc.GetJSObject

    jso.console.Show

    jso.console.Clear

    For i = 1 To 500

        If Cells(i, 1).Value = "" Then Exit For

        jso.console.println (Cells(i, 1).Value)

    Next i

    gApp.Show

End If

End Sub

This topic has been closed for replies.

1 reply

Bernd Alheit
Community Expert
Community Expert
August 18, 2017

Don't use the console. You can use ExecuteThisJavaScript for this:

How to execute javascript code in VBScript?

Known Participant
August 18, 2017

Thanks for the info. I still can't quite get it to work though. I have my string variable loaded with my script, but for some reason it says I can't use my jso as the object like this

jso.Fields.ExecuteThisJavaScript ~Script variable~

Any ideas on how to run this JS on the document that I opened with the OpenDoc filepath in my code above?

Bernd Alheit
Community Expert
Community Expert
August 18, 2017

Why do you use jso? Did you read the content of the posted link?