2 Replies Latest reply on Sep 9, 2008 12:34 PM by ReinhardF

    How to execute javascript code in VBScript?

      I'd like to execute similar javascript code for some particular PDF files(parameters for pagenumber are quite different),such as:

      this.setPageLabels(0, [ "A", "", 3] );
      this.setPageLabels(1, [ "r", "", 1] );
      this.setPageLabels(16, [ "D", "", 1] );

      How to write the code in VBScript?

      Thanks in advance!

      P.S. I try to add code but end up with error messages

      =================================================================
      path = "E:\test.pdf"

      '//Write the first and last pagenumbers and label to a txt.

      set oWs = CreateObject ("Wscript.Shell")
      set fs = CreateObject("Scripting.FileSystemObject")
      oWs.run "Acrobat.exe"
      While not oWs.AppActivate("Adobe Acrobat") : Wscript.Sleep 1000 : Wend

      Set objArgs = WScript.Arguments
      if objArgs.Count = 1 then path = ObjArgs(0)

      Set gApp = CreateObject("AcroExch.App")
      Set AVDoc = CreateObject("AcroExch.AVDoc")
      If (AVDoc.Open(Path,"")) Then
      Set AVDoc = gApp.GetActiveDoc()
      Set PDDoc = AVDoc.GetPDDoc()
      Set JSO = PDDoc.GetJSObject

      '// Write Bookmark JS to a variable
      Ex = " var vTOC = """"; "&vbLF _
      & " var s = """"; "&vbLF _
      & " var i=0; "&vbLF _
      & " p = i+1; "&vbLF _
      & " vTOC += ( this.getPageLabel(i) +""|""); "&vbLF _
      & " i= this.numPages-1 ; "&vbLF _
      & " p = i+1; "&vbLF _
      & " vTOC += ( this.getPageLabel(i) +""|""+p+""|""); "

      ' // Execute the JavaScript
      Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
      AForm.Fields.ExecuteThisJavaScript EX

      ' // Write Bookmarks to CSV (PageNr;Level;Text)
      Set JSO = PDDoc.GetJSObject '//->renew JSO for some unknown reasons
      xf = jso.vToc

      Set objIE = CreateObject("InternetExplorer.Application")
      objIE.Navigate("about:blank")
      objIE.document.parentwindow.clipboardData.SetData "text", xf
      objIE.Quit

      CsvFN = left(path, InstrRev(path, ".")-1) &"_page.txt"
      set FL = fs.OpenTextFile(CsvFN, 2, true) '//-> 2=ForWriting
      xWr = split(xf,"|")
      for x=0 to UBound(xWr)
      FL.WriteLine xWr(x)
      next
      FL.Close
      Else
      MsgBox "Failed to Open: " & Path
      End If

      Set JSO = Nothing
      Set PDDoc = Nothing
      Set AVDoc = Nothing
      Set AForm = Nothing
      Set gApp = Nothing
        • 1. Re: How to execute javascript code in VBScript?
          Patrick Leckey Level 3
          What error are you getting? What version of Acrobat do you have installed on the machine? Do you only have Reader installed?
          • 2. Re: How to execute javascript code in VBScript?
            ReinhardF Level 3
            If I remember right, I wrote this script a while back to demonstrate how java script can be direct executed and how you can get back the results. Didn't I wrote a commentary to that?

            However the parts you need:

            ' // Write JS code from help file or whatever to a VBS variable:

            JsFromHelp = " this.setPageLabels(0, [ ""A"", """", 3] ); "&vbLF _
            & " this.setPageLabels(1, [ ""r"", """", 1] ); "&vbLF _
            & " this.setPageLabels(16, [ ""D"", """", 1] ); "

            ' // Execute the JavaScript
            Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
            AForm.Fields.ExecuteThisJavaScript JsFromHelp

            From the rest keep only:
            end if

            set .. = nothing

            Untested, but should work - as far as you have a little bit more then only Reader.

            HTH, Reinhard