3 Replies Latest reply on Dec 7, 2014 1:17 PM by CarlosCanto

    Adobe Javascript from within Excel VBA

    Willis77019b Level 1

      I have some limited experience with Excel VBA and Adobe Javascript separately, but have never tried to put them together. I am rather baffled by the use of “DoJavaScript” from within Excel VBA. I have a number of Illustrator JavaScripts that do various tasks (mostly measure and set properties on illustrator drawing objects to determine some geometric metrics). I wanted to run these scripts from within Excel VBA (they run without problem when directly called into Illustrator). My hope was to pass a few real numbers from VBA into the JavaScript before one ran to set some run parameters and then get a few real numbers back out from the JavaScript back into VBA. I cannot seem to get even the simple DoJavaScript command given in the Adobe VBscript reference to run. Reading other posts that seem somewhat related it is not clear if I am doing something very simple wrong, or if my understanding of the use of the DoJavaScript command is misguided. Anyone able to shed some light on this?

      My last try was this simple test...almost directly from the Adobe reference:

       

      Private Sub SetScale_CommandButton1_Click()

      Set appRef = CreateObject("Illustrator.Application")

      Set myNumberOfDocuments = appRef.DoJavaScript("documents.length;")

      MsgBox myNumberOfDocuments

      End Sub

       

      I am really hoping to be able to run javascripts that are a few hundred lines long....but if I cannot even get something simple to go then I am about to give up on it.

      Thanks for any suggestions

        • 1. Re: Adobe Javascript from within Excel VBA
          CarlosCanto Adobe Community Professional & MVP

          I got type mismatch error, to make your sample work, simply remove the second "set"

           

          Private Sub SetScale_CommandButton1_Click()

               Set appRef = CreateObject("Illustrator.Application")

               myNumberOfDocuments = appRef.DoJavaScript("documents.length;")

               MsgBox myNumberOfDocuments

          End Sub

          • 2. Re: Adobe Javascript from within Excel VBA
            Willis77019b Level 1

            Carlos;

            I am most grateful! ... I was stumped on that for hours, thought I had tried about every combinatory possible, and was about to give up on getting the DoJavaScript  method to work. There are few examples of this on the web, so for anyone else who would like a bit more verbose  example I post below is a simple VBA sub-routine that uses a javascript function to return the number of selected objects in the current active illustrator document and displays that number in a message box in excel. Simple example, but it was the sort if thing I was looking for all over the web before Carlos’ help.

            Cheers,

            Brian

             

            Private Sub ObjectNum()

            Set appRef = CreateObject("Illustrator.Application")

                  tmp$ = "function myfunction() {"

                  tmp$ = tmp$ + "var idoc = app.activeDocument;"

                  tmp$ = tmp$ + "var sel = idoc.selection;"

                  tmp$ = tmp$ + "var numobj = sel.length;"

                  tmp$ = tmp$ + "return numobj;};"

                  tmp$ = tmp$ + "myfunction();"

            numobj = appRef.DoJavaScript(tmp$)

            MsgBox numobj

            End Sub

            • 3. Re: Adobe Javascript from within Excel VBA
              CarlosCanto Adobe Community Professional & MVP

              you're welcome, thanks for posting a working sample using a functions.