12 Replies Latest reply on Jul 7, 2012 11:31 AM by CarlosCanto

    Can I call an InDesign script from an Excel Macro

    gnits999 Level 1

      I am new to InDesign scripts, but making good progress.


      I have an InDesign Javascript which works perfectly if I call it from inside InDesign or if I double click on the script, say on the desktop.


      The data (text) required by the script is prepared within Excel by using an Excel macro.


      I would love to be able to run the InDesign script  from the Excel macro to save a lot of keystrokes (ie Open InDesign, Open Script Panel, Select Script...etc).


      I have explored such wonders as Shell, wscript.exe, DoScript... but cannot get it to work.


      Can anyone supply me with an example of the vital few lines of code / syntax.


      InDesign 6 , Win 7 Office 2010



        • 1. Re: Can I call an InDesign script from an Excel Macro
          Fred Goldman Level 3

          Run a Powershell script from the macro using Shell() that launches InDesign and make your InDesign script a startup script.

          • 2. Re: Can I call an InDesign script from an Excel Macro
            gnits999 Level 1

            Ok...thanks.     ... but how do I stop the StartUp script running when I want to use InDesign for another  task.

            • 3. Re: Can I call an InDesign script from an Excel Macro
              Dirk Becker Level 4

              I can't provide working steps, as I haven't had the urge to start up Windows in years.


              Assuming you use VBA, a little internet search on the keywords Excel VBA COM TLB should give you some hints: you first have to load the "Reference" to the InDesign TLB type library file from within your VBA Editor. Now it should be possible to use CreateObject to obtain the equivalent of the InDesign JavaScript "app" instance. Then use DoScript to run your script. Don't forget to specify the language, default for app.doScript() is to assume the same language.


              The InDesign Scripting Tutorial also has some steps, search for TLB in

              http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/indesign/cs55-docs/In DesignScripting/InDesign-ScriptingTutorial.pdf



              • 4. Re: Can I call an InDesign script from an Excel Macro
                gnits999 Level 1

                Dirk, I see the broad direction you outline and have found the  references in the Tutorial, but for me at the moment, diving into the Object model is still beyond my current capacity, especially mixing Javascript and VBA syntax.


                I was hoping for a few simple lines of code.  Thanks for the direction.

                • 5. Re: Can I call an InDesign script from an Excel Macro
                  CarlosCanto Adobe Community Professional & MVP

                  add a reference to the Windows Script Host, then insert the following in your macro anywhere you're ready to run your javascript


                  Dim ws As New IWshRuntimeLibrary.WshShell
                  ws.Run ("C:\Users\carlos\Documents\Adobe Scripts\yourScript.jsx") 'change to your own path


                  the only issue is, it does not seem to allow spaces...so you have to move your script to another folder where you'll get the security warning.

                  • 6. Re: Can I call an InDesign script from an Excel Macro
                    gnits999 Level 1

                    Thank you CarlosCanto.


                    Following the script above incl adding the Reference to WSH the VBA script is successfully launching InDesign and starting the script.


                    However, InDesign is hanging (when it appears opens an Indd template file).  I sense I need to add something in either the VB script or Indd script or both to .....


                    a. from an Indd perspective.... return control to the VB script


                    b. from a vba perspective ... understand when the Indd actions are complete and end the script.


                    When I run the script from my desktop (where I have the script file with no spaces in the name) the Indd script works perfectly.


                    I am so tantalisingly close to the final solution.......

                    • 7. Re: Can I call an InDesign script from an Excel Macro
                      gnits999 Level 1

                      I am following a lead to the hanging behaviour.


                      My jsx script is creating one Indd file and also exporting a low and hi-quality pdf file (3 file outputs in total).  These are stored in a particular folder.  When I run the Indd script from my desktop (ie double click on the jsx file) the script will happily overwrite the existing files (which is what I want).


                      If I have the script executed from the VBA script it will work perfectly when there are no files to overwrite but hangs if it needs to overwrite the files.  I will probably have to get the Indd script to recognise if the files exist, if so delete them ( I assume this is possible within Indd script).


                      Nearly nearly there.


                      I would still welcome any comments on this posting or post no.6.


                      Thanks to all for your help.

                      • 8. Re: Can I call an InDesign script from an Excel Macro
                        gnits999 Level 1

                        Unfortunately, post 7 above is not the answer.  When I ran the scripts again (with no files to overwrite) it hung in the same place.


                        The Jsx script is actually working to an extent. It is opening a template and actually starting a datamerge. It appears to open the first page of the new data merge document, but not populate any of the merge fields.


                        Any ideas how I can stop the jsx script so I can debug it from a known working location.


                        I have  $.bp() statements at critical places in the script (maybe that's the problem).


                        Is there a statement I can use to halt the jsx script so I can debug from a certain point when running the script from vba thru the WshShell approach.

                        • 9. Re: Can I call an InDesign script from an Excel Macro
                          [Jongware] Most Valuable Participant

                          Word exhibits a curious behavior when I run a VBA script in it that targets InDesign. While it is running, *I can still work with InDesign*! It seems VBA (or likely *any* external script) is running asynchronously. I expect Excel to do the same ...


                          If you sandwich your script commands between InDesign and Excel, you might find this to be the cause of hangs and other unexpected behavior. For instance, Excel starts a native InDesign script that ought to create a new document. Now before InDesign is ready, the Excel script continues with its next command -- attempting to copy text into this -- not yet existing! -- new file.


                          You'll have to experiment with what you do in each of the scripts. In my scenario above, you might be able to solve it by having the Excel script create the ID document, for example.


                          (Oh -- and deleting a file with Javascript is perfectly possible. Hint: the command is *not* called 'delete', because that's an internal Javascript command :)

                          • 10. Re: Can I call an InDesign script from an Excel Macro
                            gnits999 Level 1

                            I suspected all along that Excel is running ahead of itself.


                            So, I can step thru the Excel vba script and get it to execute the command to execute the Jsx script.


                            I can then see InDesign launch, open the primary Indd template, start the data merge, create the first page of the datamerge output and then stop. I still have not gone onto the next VBA instruction.


                            I am going to explore my code for any statements which  either post info to the Indd console or $.bp() type (which work fine when running the jsx script from the desktop, but may cause different behaviour when run from a different master than the Indd ExtendScript  Toolkit ).


                            I will definitely need to add something to the vba script to pause it until it gets an instruction that the Indd script has finished (another little project).


                            [Ps. I thought the challenge was to get my jsx script to do all the things I needed with my documents and images. I figured that calling this from a vba script would be a simple single line instruction and a piece of cake.  I am really really  happy with my jsx script in terms of its primary function, but need it to execute from VBA to achieve the workflow benefits.  One lives and sometimes learns.]


                            So.. are there any commands in jsx I can use to halt it in debug mode,, when not inside the Extendscript Toolkit.

                            • 11. Re: Can I call an InDesign script from an Excel Macro
                              gnits999 Level 1

                              I found the final critical problem.  As usual "doh".


                              • I am in Lightroom, have completed my edits to a bunch of images, finalised all my metadata (Titles, captions, locations,  etc).
                              • I use ListView PlugIn to generate a list of all the images I want to create Indd or PDF formatted documents for.
                              • Click Export to Excel ( a critical/ fantastic feature of Listview...watch out for issues with CSV...use HTML option).
                              • My list of images and related metadata opens in Excel.  I have my Excel macro on a command button. Click.
                              • InDesign launches, progresses and hangs at the data merge stage.


                              The problem is that I am still in the Excel workbook, which my Javascript wants to use as input for the DataMerge.  Conflict of rights to this file.

                              Solution.  Modify the XL script to close my current workbook.


                              My jsx script now works, regardless of whether I have previous copies of the output files there or not (ie will overwrite if the are present. This is what I want, as the folder will always have the lates results of this script.


                              So, I am now a very happy bunny, and grateful for all the input.  Every single response opened up options for me to progress.


                              I still have an edgy feeling that the VBA script should be waiting for the JSX action to finish and then exit smoothly. I will explore how that can be achieved.


                              Also,  the script leaves Indesign open (with all files closed....as per jsx script).  I want to figure out how to close InDesign remotely and smoothly.



                              This will leave me back in Lightroom, looking at my list of images within the ListView Plug-in....and all my document  professionaly packaged in pdf and Indd products.



                              Again, thanks to all for your contributions.  I start this last Sat with no knowledge of Javascript, ExtendScript jde, DOM's, etc.. It has been an intense week, but utterly satisfying to achieve a result and level of automation I have been looking for since pre-Lightroom days.


                              I have been really impressed with the contributions from the InDesign scripting community. I include in this John Beardsworth , who responded very quickly to an issue within his plug-in (List View).


                              Thanks again.


                              Yours Sincerely,



                              • 12. Re: Can I call an InDesign script from an Excel Macro
                                CarlosCanto Adobe Community Professional & MVP

                                congrats, happy to see it worked out good.


                                to close the ID application, add the following line at the end of your jsx script