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

Adobe Javascript from within Excel VBA

Explorer ,
Dec 06, 2014 Dec 06, 2014

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

TOPICS
Scripting
1.7K
Translate
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

correct answers 1 Correct answer

Community Expert , Dec 06, 2014 Dec 06, 2014

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

Translate
Adobe
Community Expert ,
Dec 06, 2014 Dec 06, 2014

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

Translate
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
Explorer ,
Dec 07, 2014 Dec 07, 2014

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

Translate
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 ,
Dec 07, 2014 Dec 07, 2014
LATEST

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

Translate
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