Using excel macro to run JS code in Acrobat Pro
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Don't use the console. You can use ExecuteThisJavaScript for this:
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Why do you use jso? Did you read the content of the posted link?
Copy link to clipboard
Copied
I did read the content of the posted link. Here's what I have:
Sub Open_Acrobat()
Dim OpenDoc As String
Dim gApp As AcroApp
Dim gPDDoc As Acrobat.AcroPDDoc
Dim AForm As Object
Dim JSExcelDoc As String
OpenDoc = Application.WorksheetFunction.Concat(Cells(2, 3).Value, "\", Cells(2, 2).Value, ".pdf")
Set gApp = CreateObject("AcroExch.App")
Set gPDDoc = CreateObject("AcroExch.PDDoc")
If gPDDoc.Open(OpenDoc) Then
For i = 1 To 500 'Creates my JS from the contents of these cells, creates JSExcelDoc string variable that stores that script
If Cells(i, 1).Value <> "" Then
JSExcelDoc = JSExcelDoc & " " & Cells(i, 1).Value 'Forms a concatenated JS
End If
Next i
Set AForm = CreateObject("AFormAut.App")
AForm.Fields.ExecuteThisJavaScript JSExcelDoc
End If
End Sub
The Error that it retuns on the "ExecuteThisJavaScript" line says that no document is open in acrobat viewer. However, the first line of my If statement opens the document that I want to work with. How can I make the script run on that document?
Copy link to clipboard
Copied
I think the issue is with my object. How do I get the proper object for the open document?
Copy link to clipboard
Copied
shallberg7 wrote
I did read the content of the posted link. Here's what I have:
...
You doesn't read it. Where do you use AVDoc.Open ?

