• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using excel macro to run JS code in Acrobat Pro

Community Beginner ,
Aug 17, 2017 Aug 17, 2017

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

TOPICS
Acrobat SDK and JavaScript

Views

3.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2017 Aug 18, 2017

Copy link to clipboard

Copied

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

How to execute javascript code in VBScript?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2017 Aug 18, 2017

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2017 Aug 18, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2017 Aug 18, 2017

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2017 Aug 18, 2017

Copy link to clipboard

Copied

I think the issue is with my object. How do I get the proper object for the open document?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2017 Aug 18, 2017

Copy link to clipboard

Copied

LATEST

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines